문제

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

도움이 되었습니까?

해결책

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top