iPhone dev - performSelector: withObject: afterDelay ou NSTimer?
Para repetir uma chamada de método (ou envio de mensagem, eu acho que o termo apropriado é) cadax segundos, é melhor usar um NSTimer (ScheduledTimerWithTimeInterval do NSTimer: target: selector: userInfo: repete :) ou para que o método se chame recursivamente no final (usando performSelector: withObject: afterDelay)? O último não usa um objeto, mas talvez seja menos claro / legível? Além disso, só para ter uma ideia do que estou fazendo, é apenas uma visualização com um marcador que conta até às 12:00 da meia-noite, e quando chegar a 0, ele piscará o horário (00:00:00) e tocar um sinal sonoro para sempre.
Obrigado.
Edit: também, qual seria a melhor maneira de reproduzir repetidamente um SystemSoundID (para sempre)? Edit: acabei usando isso para jogar o SystemSoundID para sempre:
// 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
Parece funcionar bem .. E para o rótulo piscando eu vou usar um NSTimer eu acho.