【iOS】NSTimerの使い方
iOSのSDKには、処理を待たすウェイトのような関数はないのかな?ということで、タイマーにて実現してみた。
startTimerを呼び出すと、3秒後にtickを呼び出すようにするには、
NSTimer *aTimer;
-(void)startTimer
{
if (aTimer) {
[self stopTimer];
}
aTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
-(void)stopTimer
{
if (aTimer) {
[aTimer invalidate];
aTimer = nil;
}
}
-(void)tick:(NSTimer*)theTimer
{
if (aTimer) {
[self stopTimer];
}
//実際のタイマーイベントロジック
}
関連記事