-1

我想在-touchesEnded方法完成执行后 2 秒执行一个-recognized方法。但是,如果用户在这 2 秒内触摸了某物,则不得执行该方法。在下一次再次执行-touchesEnded方法后,必须再次设置计时器以等待 2 秒。等等......希望这个问题足够清楚。如果没有,请告诉我。

4

3 回答 3

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
}
于 2013-04-02T18:18:54.053 回答
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\");
    }
于 2013-04-02T18:22:44.450 回答
0

事实证明,这真的很简单。

在 view.h 中创建一个 ivar

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 !");
        }
    }
}

奇迹般有效。

于 2013-04-03T14:57:29.663 回答