UIPanGestureRecognizer does not switch to state “End” or “Cancel” if user panned x and y in negative direction

坚强是说给别人听的谎言 提交于 2019-12-21 07:22:55

问题


I've got a little problem with the UIPanGestureRecognizer. The Recognizer does not report the UIGestureRecognizerStateEnded state if the user panned to the top left (means negative x and y directions)

The state changes to UIGestureRecognizerStateEnded if any direction is positive when the user lifts his finger, but it just ceases to report actions if both directions are negative or zero.

This is bad because i hide some overlay views as long as the user drags something around and those views do not return in failure case.

Of course I could setup a NSTimer to display the overlay after some time automatically again but i can see no obvious error in my code and I want it clean.

Is there something i missed? Is it an Apple Bug?

Initialization is like this:

pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
[self addGestureRecognizer:pan];
[pan release];

The handling function looks like this:

- (void)panRecognized:(UIPanGestureRecognizer *)gestureRecognizer {
    switch ([gestureRecognizer state]) {
        case UIGestureRecognizerStateBegan:
            // fade some overlaying views out
            break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            // fade in the overlays
            break;
        default:
            break;
    }

    // handle panning
}

回答1:


The line

[self addGestureRecognizer:pan];

looks wrong to me.

It seems like you are creating the gesture recognizer from inside a UIView and not a UIViewController. So if the view is dealloc both it and the gesture recognizer will disappear.

Better to create the gesture recognizer from the UIViewController. Also the UIViewController needs to keep a strong point to the recognizer.



来源:https://stackoverflow.com/questions/4461621/uipangesturerecognizer-does-not-switch-to-state-end-or-cancel-if-user-panned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!