Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pan gesture translation being reset between should-begin and handler (with state began) on iOS7

I have a pan gesture recognizer to drag a panel up, down, left, or right. When the direction of the pan is not possible I don't allow the recognizer to begin so that the touches can go to other UI elements within the panel.

However, on iOS7 the translation sometimes gets reset between gestureRecognizerShouldBegin: and my gesture handler handlePan:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer*)panGR
{
    CGPoint trans = [panGR translationInView:self.view];
    NSLog(@"should begin trans: (%.2f, %.2f)", trans.x, trans.y);
    ...

This logs: should begin trans: (18.00, 0.00)

- (void)handlePan:(UIPanGestureRecognizer*)panGR
{
    CGPoint trans = [panGR translationInView:self.view];

    switch(panGR.state)
    {
        case UIGestureRecognizerStateBegan:
            NSLog(@"handlePan began trans: (%.2f, %.2f)", trans.x, trans.y);
            ...

This logs: handlePan began trans: (0.00, 0.00)

Which means the shared code to determine the direction of the pan (right, in this case) works in gestureRecognizerShouldBegin: and allows the gesture to begin, but then can't be determined in handlePan: when the state is UIGestureRecognizerStateBegan.

Is this a bug in iOS7 or has the behaviour deliberately changed to accommodate new gesture types? Also, can anyone suggest a good way to work around this issue?

like image 393
jhabbott Avatar asked Sep 22 '13 17:09

jhabbott


People also ask

What does pan gesture mean?

The pan gesture is used for detecting the movement of fingers around the screen and applying that movement to content, and is implemented with the PanGestureRecognizer class.

How to use pan gesture?

The user must press one or more fingers on a view while panning on the screen. A panning gesture is on continuous action when the user moves one or more fingers allowed (minimumNumberOfTouches) to enough distance for recognition as a pan.

What is translation in Swift?

As an easy-to-use, standalone translation solution, SWIFT Translator easily defines, maps and validates messages from and to any format. This significantly reduces the time and cost associated with the development of messaging upgrades.


1 Answers

The UIPanGestureRecognizer is always setting the translation to (0,0) after reaching the UIGestureRecognizerStateBegan state - since even the slightest translation is recognized it's designed to work as a trigger only (if you set up large threshold for beginning the recognition like (50,50) you will obviously get a "lag" in UI behavior - as a workaround I would suggest to store the value of the translation and then use a UIView animation to pan the object more smoothly). You should use the UIGestureRecognizerStateChanged for updating the translation, and UIGestureRecognizerStateRecognized for setting the end point of the pan.

like image 162
Wladek Surala Avatar answered Sep 21 '22 11:09

Wladek Surala