我想执行一个方法-recognized在-touchesEnded方法执行完成后2秒钟。但是,如果用户在这2秒钟内触摸了某些东西,则不能执行该方法。在下次再次执行方法后,必须再次设置计时器以等待2秒钟。等等...希望这个问题很清楚。如果没有,请让我知道。执行延迟后的方法
Q
执行延迟后的方法
-1
A
回答
2
您需要使用NSTimer来协调这一点。当您要启动计时器的事件触发时,请使用scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
在两秒钟内安排函数调用。
使用视图控制器全局的布尔变量来防止定时器置于其间。
这是一个粗略的想法:
BOOL shouldRespondToTouch = YES;
- (void)touchesEnded {
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(doAction) userInfo:nil repeats:NO];
shouldRespondToTouch = NO;
}
- (void)doAction {
shouldRespondToTouch = YES;
// Do stuff here
}
0
-(void) touchesEnded {
[NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];
}
//define the targetmethod
-(void) targetMethod: NSTimer * theTimer
{
NSLog(@\"Me is here at 1 minute delay\");
}
0
事实证明,这是非常简单的。
在view.h
NSDate *startDate;
创建一个实例变量添加以下这些方法在view.m
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self->startDate = [NSDate date];
NSLog(@"%@", startDate);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self->startDate) {
NSTimeInterval ti = [[NSDate date] timeIntervalSinceDate:self->startDate];
NSLog(@"Time: %f", ti);
if (ti >= 2) {
NSLog(@"Yes, greater than 2 seconds !");
}
}
}
就像一个魅力。
相关问题
- 1. 从方法和延迟执行
- 2. 延迟方法执行命令
- 3. 延迟执行
- 4. UIButton的IBAction方法在Swift中执行延迟后
- 5. 执行延迟后的方法并取消它
- 6. 延迟后调用方法?
- 7. 延迟后可以执行类方法吗?
- 8. 如何在线程延迟之后优先执行方法?
- 9. 完全神秘 - 随机延迟后执行iOS方法
- 10. 使用ThreadPool安排延迟执行方法的最佳方法?
- 11. 延迟执行IRP_MN_SET_POWER
- 12. LINQ延迟执行
- 13. AS3延迟执行?
- 14. PLINQ延迟执行
- 15. 一个方法中执行方法无延迟
- 16. 延迟后执行操作iPhone
- 17. 延迟然后执行任务
- 18. 延迟后执行选择器
- 19. 执行。然后不加延迟
- 20. C#的LINQ - 延迟执行
- 21. Jquery的延迟执行
- 22. VB.NET中的延迟执行
- 23. 执行segue的延迟iOS
- 24. jQuery的示例按顺序执行代码的延迟方法
- 25. 延迟onPause()方法
- 26. Javascript代码执行延迟
- 27. jquery延迟功能执行
- 28. Iphone延迟执行代码
- 29. 延迟uart命令执行
- 30. jquery延迟执行代码
谢谢@woz,+1。但是,我发现了一个可能更好的方法。 – m177312