Aby powtórzyć wywołanie metody (lub wysłać wiadomość, myślę, że odpowiedni termin jest) co sekund x, czy lepiej użyć NSTimer (NSTimer's zaplanowaneTimerWithTimeInterval: cel: selector: userInfo: Powtarza :) lub mieć metodę rekursywnie wywołuje się na końcu (używając performSelector: withObject: afterDelay)? Ten ostatni nie używa obiektu, ale może jest mniej czytelny/czytelny? Ponadto, aby dać ci wyobrażenie o tym, co robię, jest to tylko widok z etykietą, która odlicza do północy 00, a kiedy osiągnie 0, będzie migać czas (00:00:00) i odtwarzaj sygnał dźwiękowy na zawsze.iPhone dev - performSelector: withObject: afterDelay lub NSTimer?
Dzięki.
Edycja: także, jaki byłby najlepszy sposób na wielokrotne odtwarzanie SystemSoundID (na zawsze)? Edit: I skończyło się to grać SystemSoundID 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
wydaje się działać dobrze .. I na etykiecie migającym będę używać NSTimer chyba.
Jest to przydatne do kontrastowania obu metod. –