iPhone dev - performSelector: withObject: afterDelay lub NSTimer?
Aby powtórzyć wywołanie metody (lub wysłanie wiadomości, myślę, że odpowiedni termin jest) cox sekund, czy lepiej jest użyć NSTimer (NSTimer's scheduleTimerWithTimeInterval: target: selector: userInfo: repeats :) lub czy metoda ma rekurencyjnie wywoływać się na końcu (używając performSelector: withObject: afterDelay)? Ten ostatni nie używa obiektu, ale może jest mniej czytelny / czytelny? Po prostu, aby dać ci wyobrażenie o tym, co robię, jest to tylko widok z etykietą, która odlicza do północy do północy, a kiedy osiągnie 0, będzie migać czas (00:00:00) i odtworzyć dźwięk dźwiękowy na zawsze.
Dzięki.
Edytuj: również, jaki byłby najlepszy sposób wielokrotnego odtwarzania SystemSoundID (na zawsze)? Edytuj: Skończyło się na tym, że grałem w SystemSoundID na zawsze:
// Utilities.h
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioServices.h>
static void soundCompleted(SystemSoundID soundID, void *myself);
@interface Utilities : NSObject {
}
+ (SystemSoundID)createSystemSoundIDFromFile:(NSString *)fileName ofType:(NSString *)type;
+ (void)playAndRepeatSystemSoundID:(SystemSoundID)soundID;
+ (void)stopPlayingAndDisposeSystemSoundID;
@end
// Utilities.m
#import "Utilities.h"
static BOOL play;
static void soundCompleted(SystemSoundID soundID, void *interval) {
if(play) {
[NSThread sleepForTimeInterval:(NSTimeInterval)interval];
AudioServicesPlaySystemSound(soundID);
} else {
AudioServicesRemoveSystemSoundCompletion(soundID);
AudioServicesDisposeSystemSoundID(soundID);
}
}
@implementation Utilities
+ (SystemSoundID)createSystemSoundIDFromFile:(NSString *)fileName ofType:(NSString *)type {
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:type];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
return soundID;
}
+ (void)playAndRepeatSystemSoundID:(SystemSoundID)soundID interval:(NSTimeInterval)interval {
play = YES
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL,
soundCompleted, (void *)interval);
AudioServicesPlaySystemSound(soundID);
}
+ (void)stopPlayingAndDisposeSystemSoundID {
play = NO
}
@end
Wygląda na to, że działa dobrze. I na miganie etykiety użyję NSTimer, jak sądzę.