同时使用触摸按下和触摸抬起手势识别器

4

我在我的UIView上使用了两个手势识别器。一个是标准的UITapGestureRecognizer,另一个是我编写的非常简单的触摸按下识别器:

@implementation TouchDownGestureRecognizer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.state == UIGestureRecognizerStatePossible) {
        self.state = UIGestureRecognizerStateRecognized;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    self.state = UIGestureRecognizerStateFailed;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    self.state = UIGestureRecognizerStateFailed;
}

@end

只有在我为它们分配一个包含此方法的委托时,它们才能一起工作:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

一切正常运作,但是当我在该视图上进行长按时,按下触摸识别器会触发,而抬起触摸识别器则不会。对于短时间的点击,两个都可以被触发。

我实现了UIGestureRecognizerDelegate中的所有方法都返回了YES,但仍然无济于事。如果我添加日志以查看与委托的交互及自己的识别器内部情况,我可以看到对于短时间和长时间的点击,调用顺序是相同的——除了抬起触摸识别器的调用。我做错了哪些事情吗?

1个回答

6

为什么不直接从UILongPressGestureRecognizer中检查touchUp

-(void)selectionDetected:(UILongPressGestureRecognizer*)longPress
{
    if(longPress.state==1)
    {
       //long Press is being held down
    }
    else if(longPress.state==3)
    {
        //the touch has been picked up
    }
}

1
非常好的建议,谢谢!那个识别器需要将 minimumPressDuration 设置为 0,现在可以正常工作了。 - Sergey Mikhanov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接