2011-09-29 109 views
0

我有一个名为touchStatus的变量,用于跟踪程序中的触摸状态。 touchesBegan方法中的变量设置为B,touchesEnded中的变量设置为E,touchesMoved中的变量设置为M延迟触摸响应

但是,我的要求有点不同。我被要求以某种方式进行编程,以便手指离开屏幕和touchStatus设置为E之间存在一秒的延迟。如果用户在一秒钟之前触摸屏幕,则touchStatus应继续为MB(无论是在一秒之前)。

我该如何做到这一点?

回答

1

您可以使用

[self performSelector:@selector(setEndedValue:) withObject:self afterDelay:1.0]; 

创建一个BOOL监视是否值应如设置:

BOOL hasTouchRestarted = NO; 

如果设置值前的画面再次感动,将值更改为YES并从setEndedValue方法返回。

-(void)setEndedValue { 
    if (hasTouchRestarted) { return; } 
    // set value 
    self.touchStatus = E; 
} 
+0

啊!非常感谢。那太简单了。就像下面的一些人建议的那样,我试图使用定时器,但那是太多的代码。无论如何,'performSelector:withObject:afterDelay'就像一个魅力。我以前应该想到这个! – Ravi

+0

当然可以!定时器非常脆弱,如果你没有正确地使它们失效,你可以同时运行多个定时器。 PerformSelector为你处理所有这些。 – seejaneworkit

0

在touchEnded例程中,设置NSTimer任务来在一秒钟内调用选择器。如果在此之前还有其他触摸,请取消定时器任务。

0

使用NSTimer *计时器伊娃发起延迟呼叫,如果用户在一秒钟之前提起手指,则取消呼叫。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.myvar = @"B"; 
    [self.timer invalidate]; 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleOneSecondPress) userInfo:nil repeats:NO]; 
} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event { 
    [self.timer invalidate]; 
    self.timer = nil; 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.myvar = @"M"; 
} 
- (void)handleOneSecondPress { 
    self.timer = nil; 
    self.myvar = @"E"; 
}