要重复一个方法调用(或消息发送,我想适当的任期)每x秒,是它更好地使用一个NSTimer(的NSTimer的scheduledTimerWithTimeInterval:目标:选择:用户信息:重复:)或有方法递归调用自身在端部(使用performSelector:withObject:afterDelay)? 后者不使用对象,但也许它的不清晰/可读性? 此外,只给你的我在做什么,与倒计时至午夜12时,并且当它到达0,它会闪烁的时间标签它只是一个视图的想法(00:00:00)永远播放提示音。
谢谢。
编辑:也,这将是反复播放一个SystemSoundID(永远)的最佳方法是什么? 编辑:我结束了使用可以永远这样继续玩SystemSoundID:
// 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
似乎很好地工作。而对于标签闪烁,我将使用一个NSTimer我猜。