Cocoa touch 可可触摸计时器
如何制作一个从3开始倒计时然后运行方法的计时器?我该怎么做 这与计时器从0到3计数不同吗?无论如何,它仍将等待3秒钟Cocoa touch 可可触摸计时器,cocoa-touch,nstimer,Cocoa Touch,Nstimer,如何制作一个从3开始倒计时然后运行方法的计时器?我该怎么做 这与计时器从0到3计数不同吗?无论如何,它仍将等待3秒钟 [NSTimer scheduledTimerWithTimeInterval:3.0目标:自选择器:@selector(myMethod:)userInfo:nil repeats:NO]这与从0到3的计时器计数不同吗?无论如何,它仍将等待3秒钟 [NSTimer scheduledTimerWithTimeInterval:3.0目标:自选择器:@selector(myMet
[NSTimer scheduledTimerWithTimeInterval:3.0目标:自选择器:@selector(myMethod:)userInfo:nil repeats:NO]代码>这与从0到3的计时器计数不同吗?无论如何,它仍将等待3秒钟
[NSTimer scheduledTimerWithTimeInterval:3.0目标:自选择器:@selector(myMethod:)userInfo:nil repeats:NO]代码>更好的方法可能是使用性能选择器:withObject:afterDelay:
方法:
[self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0f];
或者,如果方法采用1个参数:
[self performSelector:@selector(myMethod:) withObject:parameter afterDelay:3.0f];
如果方法需要多个参数,则需要使用NSInvocation
class更好的方法可能是使用performSelector:withObject:afterDelay:
方法:
[self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0f];
- (void) handleTimer: (NSTimer *) timer
{
do some work here...
} // handleTimer
// at some point in your controller
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 3.0
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: NO];
或者,如果方法采用1个参数:
[self performSelector:@selector(myMethod:) withObject:parameter afterDelay:3.0f];
如果方法需要多个参数,则需要使用NSInvocation
class我选择此作为接受答案,因为它需要最少的代码。:]请注意,如果您希望在延迟结束之前取消或停止选择器的执行,您可能需要查看下面slf的答案。我选择此答案作为已接受的答案,因为它需要最少的代码。:]请注意,如果您希望在延迟过去之前取消或停止选择器的执行,您可能需要查看下面的slf答案。
- (void) handleTimer: (NSTimer *) timer
{
do some work here...
} // handleTimer
// at some point in your controller
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 3.0
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: NO];