Dou*_*ith 3 objective-c nstimer ios ios5 ios6
我希望每隔2秒就会出现一些不同的东西,10次.我将如何在Objective-C中实现这一目标?
我正在考虑使用NSTimer并在这么多秒后使其失效,就像上面的例子在我启动计时器后2*10秒.或者有没有办法测量蜱?
或者我正在考虑使用for循环并使用performSelector:withDelay:方法.
哪个更好?
使用的NSTimer并设置时间interval为2 seconds和repeats到YES.
计算它触发的次数.Invalidate 当它达到10.Thats它
码:
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(trigger:) userInfo:yourObject repeats:YES];
- (void)trigger:(NSTimer *)sender{
id yourObject = sender.userInfo;
static int count = 1;
@try {
NSLog(@"triggred %d time",count);
if (count == 10){
[sender invalidate];
NSLog(@"invalidated");
}
}
@catch (NSException *exception)
{
NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
@finally {
count ++;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3712 次 |
| 最近记录: |