x秒ごとにメソッド呼び出し(またはメッセージ送信、適切な用語だと思います)を繰り返すには、NSTimer(NSTimerのscheduledTimerWithTimeInterval:target:selector:userInfo:repeats:)を使用するか、メソッドに再帰的に自分自身を呼び出すようにします最後に (performSelector:withObject:afterDelay を使用)? 後者はオブジェクトを使用しませんが、明確で読みにくいのではないでしょうか? また、私が何をしているのかを説明するために、これは真夜中の 12:00 までカウントダウンするラベル付きのビューであり、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 を使用します。