我正在检测用户是否按下了 2 秒:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 2.0;
[self addGestureRecognizer:longPress];
[longPress release];
这就是我处理长按的方式:
-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer{
NSLog(@"double oo");
}
当我按下超过 2 秒时,文本“double oo”会打印两次。为什么是这样?我该如何解决?
最佳答案
UILongPressGestureRecognizer 是一个连续事件识别器。您必须查看状态,看看这是事件的开始、中间还是结束,并采取相应的行动。即您可以在开始后丢弃所有事件,或者仅根据需要查看运动。来自 Class Reference :
Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.
现在您可以像这样跟踪状态
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
//Do Whatever You want on End of Gesture
}
else if (sender.state == UIGestureRecognizerStateBegan){
NSLog(@"UIGestureRecognizerStateBegan.");
//Do Whatever You want on Began of Gesture
}
}
关于iphone - 按下时 UILongPressGestureRecognizer 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3319591/