UIPanGestureRecognizer does not switch to state “E

2019-04-04 08:06发布

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条回答
虎瘦雄心在
2楼-- · 2019-04-04 08:24

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.

查看更多
登录 后发表回答