我在视图中有此手势代码

- (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);
    }


}

但它不能正常工作,我也不知道问题出在哪里...
如果滑动是从底部到顶部,它会计算出一个距离,如果相反,则会计算出很大的距离,我不明白

最佳答案

将这些点定义为静态,否则touchBegan点将失去其值。发生这种情况是因为设置每个点的值是在不同的方法调用中进行的,并且对于每个方法,您都需要在开始时重新定义这些点。

static CGPoint touchBegan;
static CGPoint pointEnd;

10-06 13:48