开发者

How to cancel/reset an UIGestureRecognizer

开发者 https://www.devze.com 2023-03-17 20:28 出处:网络
how can I cancel or reset an UIGestureRecognizer? The problem is, that if I set waitForSomething to NO during a gesture, the next event is UIGestureRecognizerStateChanged. But the first event should b

how can I cancel or reset an UIGestureRecognizer? The problem is, that if I set waitForSomething to NO during a gesture, the next event is UIGestureRecognizerStateChanged. But the first event should be UIGestureRecognizerStateBegan.

My Code:

- (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 UIGestureRecognize开发者_运维技巧rStateChanged:
            // do something
            break;
    }
}

Thank you for you help!


I got it! :-)

Maybe someone else runs in this problem, here is the solution:

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

The next event will be UIGestureRecognizerStateFailed followed by UIGestureRecognizerStateBegan.


Swift 4:

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


If you need to do this in more than one place:

in Swift

extension UIGestureRecognizer
{
    func cancel() {
        isEnabled = false
        isEnabled = true
    }
}
0

精彩评论

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

关注公众号