Question

I have this code for gesture in a view

- (void)rightSwipeHandle:(UIPanGestureRecognizer*)gestureRecognizer{

    CGPoint touchBegan;
    CGPoint pointEnd;

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) 
    {   
        CGPoint touchBegan = [gestureRecognizer locationInView: gestureRecognizer.view];
        NSLog(@"pointBegan:%@",NSStringFromCGPoint(touchBegan));
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateChanged)
    {
    }

    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded     ||
             gestureRecognizer.state == UIGestureRecognizerStateCancelled ||
             gestureRecognizer.state == UIGestureRecognizerStateFailed)
    {   
        pointEnd = [gestureRecognizer locationInView:gestureRecognizer.view];
        NSLog(@"pointEnd:%@", NSStringFromCGPoint(pointEnd));

        CGFloat xDist = (pointEnd.x - touchBegan.x);
        CGFloat yDist = (pointEnd.y - touchBegan.y);
        CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));
        NSLog(@"distance:%f", distance);
    }


}

but it don't work fine and I don't know where is the problem... if the swipe is from the bottom to the top, it calculate a distance and if you do the opposite it calculate a big different distance, I don'tr understand

Was it helpful?

Solution

Define the points as static, otherwise the touchBegan point will lose its value. It happens because setting the values of each point occurs in different method calls and with each one, you redefine the points at the beginning.

static CGPoint touchBegan;
static CGPoint pointEnd;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top