3

我在视图中有这个手势代码

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


}

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

4

1 回答 1

5

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

static CGPoint touchBegan;
static CGPoint pointEnd;
于 2012-02-22T15:17:31.980 回答