I want to execute a method -recognized 2 seconds after the -touchesEnded method has finished execution. However, If the user touches something within these 2 seconds, the method must not be executed. A timer must again be set up to wait for 2 seconds after the -touchesEnded method has been executed again the next time. And so on... Hope this question is clear enough. If not, do let me know.
我想在-touchesEnded方法完成执行后2秒执行一个方法 - 识别。但是,如果用户在这2秒内触摸了某些内容,则不得执行该方法。在下次再次执行-touchesEnded方法后,必须再次设置计时器等待2秒。等等......希望这个问题足够清楚。如果没有,请告诉我。
3 个解决方案
#1
2
You need to use an NSTimer to coordinate this. When the event you want to start the timer is triggered use scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
to schedule a function call in two seconds.
您需要使用NSTimer来协调它。当您想要启动计时器的事件被触发时,使用scheduledTimerWithTimeInterval:target:selector:userInfo:重复:在两秒钟内调度函数调用。
Use a Boolean variable that is global to your view controller to prevent the timer from getting set in between.
使用视图控制器的全局布尔变量,以防止在其间设置计时器。
Here is a rough idea:
这是一个粗略的想法:
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
}
#2
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\");
}
#3
0
Turns out, that it is really simple.
事实证明,这很简单。
Create an ivar in view.h
在view.h中创建一个ivar
NSDate *startDate;
Add the following to these methods in view.m
在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 !");
}
}
}
Works like a charm.
奇迹般有效。
#1
2
You need to use an NSTimer to coordinate this. When the event you want to start the timer is triggered use scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
to schedule a function call in two seconds.
您需要使用NSTimer来协调它。当您想要启动计时器的事件被触发时,使用scheduledTimerWithTimeInterval:target:selector:userInfo:重复:在两秒钟内调度函数调用。
Use a Boolean variable that is global to your view controller to prevent the timer from getting set in between.
使用视图控制器的全局布尔变量,以防止在其间设置计时器。
Here is a rough idea:
这是一个粗略的想法:
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
}
#2
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\");
}
#3
0
Turns out, that it is really simple.
事实证明,这很简单。
Create an ivar in view.h
在view.h中创建一个ivar
NSDate *startDate;
Add the following to these methods in view.m
在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 !");
}
}
}
Works like a charm.
奇迹般有效。