同时使用触摸和触摸手势识别器

时间:2013-08-05 18:22:36

标签: iphone ios uikit uigesturerecognizer

我在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 个答案:

答案 0 :(得分: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
    }
}