6
A
回答
11
下面将做你的需要:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 2
target:self
selector:@selector(handleTimer:)
userInfo:nil
repeats:NO];
然后委托函数:
-(void)handleTimer: (NSTimer *) timer
{
//code
}
2
您应该能够将NSTimeInterval
设置为2.0秒,并且应该在该时间段之后触发。你在看什么?你用来调用定时器的代码是什么?
19
的NSTimer可以使用,但另一种选择是使用performSelector:withObject:afterDelay:这基本上就像一个方法调用(消息发送)稍后发生。
这个例子将发出一个doStuff:延迟之后消息:
[self performSelector:@selector(doStuff:) withObject:self afterDelay:2];
导致得到2.0秒后调用此方法:
-(void)doStuff:(id)sender
{
/// do something
}
8
您还可以使用一些有用的代码:
NSObject + PerformBlock.h
@interface NSObject (PerformBlock)
- (void)performBlock:(void(^)(void))block waitUntilDone:(BOOL)wait;
- (void)performBlock:(void(^)(void))block afterDelay:(NSTimeInterval)delay;
- (void)performBlock:(void(^)(void))block repeatCount:(NSUInteger)repeatCount timeInteval:(NSTimeInterval)timeInterval;
@end
NSObject的+ PerformBlock.m
@interface NSObject (PerformBlockHidden)
- (void)performBlock:(void(^)(void))block;
@end
@implementation NSObject (PerformBlock)
- (void)performBlock:(void(^)(void))block {
block();
}
- (void)performBlock:(void(^)(void))block waitUntilDone:(BOOL)wait {
[self performSelector:@selector(performBlock:) onThread:nil withObject:[[block copy] autorelease] waitUntilDone:wait];
}
- (void)performBlock:(void(^)(void))block afterDelay:(NSTimeInterval)delay {
[self performSelector:@selector(performBlock:) withObject:[[block copy] autorelease] afterDelay:delay];
}
- (void)performBlock:(void(^)(void))block repeatCount:(NSUInteger)repeatCount timeInteval:(NSTimeInterval)timeInterval {
for (NSInteger repetition = 0; repetition < repeatCount; repetition++)
[self performBlock:block afterDelay:(repetition*timeInterval)];
}
@end
则只需导入NSObject的+ PerformBlock.h,并呼吁:
[myObject performBlock:^{
// Code you want to perform after 2secs
} afterDelay:2];
相关问题
- 1. iOS:获取NSTimer从现在开始5秒钟触发
- 2. iPhone开发-CGPoint&NSTimer帮助
- 3. 的NSTimer - 秒表
- 4. NSTimer秒表
- 5. 秒表 - NSTimer或NSDate
- 6. NSTimer在2意见
- 7. 每秒帧速率(FPS)在Coco2d游戏iPhone中开始下降?
- 8. Skype开始拨号并在2秒后挂断 - Android
- 9. NSTimer减少秒/毫秒的时间
- 10. Iphone NSTimer问题
- 11. 发布NSTimer iPhone?
- 12. NSTimer Lag - iPhone SDK
- 13. NSTimer iPhone问题
- 14. iPhone:的NSTimer
- 15. NSTimer Lap Time iPhone
- 16. 开始iPhone开发
- 17. 开始iPhone开发
- 18. 直接秒数创建nstimer
- 19. NSTimer只能倒数秒?
- 20. iPhone - NSTimer问题当iphone锁
- 21. 如何在开始AsyncTask后10秒内显示祝酒
- 22. 我应该能够使用NSTimer延迟2秒。怎么做?
- 23. iPhone - 的NSTimer不火
- 24. iPhone NSTimer OpenGL问题
- 25. 如何在x秒后开始观察
- 26. 开始为iPhone开发
- 27. 随机数组在2秒内变化
- 28. NSTimer立即开火
- 29. Arduino打开领先2秒,然后关闭并等待3秒,然后重新开始
- 30. 通知中心的NSTimer秒表
我没有使用过这一点,但我真的很喜欢使用的块这样的概念。 – 2013-05-22 19:30:08