开发者

Delay Touch Response

开发者 https://www.devze.com 2023-04-09 17:42 出处:网络
I have a variable called touchStatus that tracks the touch status in the progr开发者_Python百科am. The variable gets set to B in the touchesBegan method, E in touchesEnded and to M in touchesMoved.

I have a variable called touchStatus that tracks the touch status in the progr开发者_Python百科am. The variable gets set to B in the touchesBegan method, E in touchesEnded and to M in touchesMoved.

However, my requirements are a little different. I was requested to program in a way so that there is one second of delay between the finger being lift off from the screen and touchStatus getting set to E. If the user touches the screen before the one second elapses, touchStatus should continue to be M or B (whatever it was before the one second).

How can I accomplish this?


You can use

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

Create a BOOL to monitor whether the value should be set such as:

BOOL hasTouchRestarted = NO;

If the screen is touched again before the value is set, change the value to YES and return from the setEndedValue method.

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


In the touchEnded routine set up an NSTimer task to invoke a selector in one second. If there is another touch before then, cancel the timer task.


Use a NSTimer *timer ivar to initiate a delayed call, and cancel the call if the user lifts the finger before one second.

- (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";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号