如何取消/重置UIGestureRecognizer

时间:2011-07-06 08:49:27

标签: iphone objective-c ios uigesturerecognizer

如何取消或重置UIGestureRecognizer?问题是,如果我在手势期间将waitForSomething设置为NO,则下一个事件为UIGestureRecognizerStateChanged。但第一个事件应该是UIGestureRecognizerStateBegan

我的代码:

- (void) panned:(UIPanGestureRecognizer *) recognizer {
    if (waitForSomething) {
        // cancel or reset the recognizer!
        // because the next event should be UIGestureRecognizerStateBegan and not UIGestureRecognizerStateChanged
        return;
    }

    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            // important initialisation code
            break;

        case UIGestureRecognizerStateChanged:
            // do something
            break;
    }
}

谢谢你的帮助!

3 个答案:

答案 0 :(得分:39)

我明白了! : - )

也许其他人遇到了这个问题,这是解决方案:

if (waitForSomething) {
    recognizer.enabled = NO;
    recognizer.enabled = YES;
    return;
}

下一个活动将是UIGestureRecognizerStateFailed,然后是UIGestureRecognizerStateBegan

答案 1 :(得分:0)

斯威夫特4:

if waitForSomething {
    recognizer.isEnabled = false
    recognizer.isEnabled = true
    return
}

答案 2 :(得分:0)

如果您需要在多个地方进行此操作:

在Swift

 扩展UIGestureRecognizer
{
    func cancel(){
        isEnabled =假
        isEnabled = true
    }
}