获得平移手势的扫描距离

时间:2014-09-23 21:11:47

标签: objective-c uiswipegesturerecognizer

当用户在屏幕上滑动时,我希望获得实时滑动数据。这是我的代码:

-(void)initViewWithCIImage:(CIImage*) ciImage UIImage:(UIImage*)uiimage
{
    _originalUIImage = uiimage;
    _originalCIImage = ciImage;
    CIContext *context = [CIContext contextWithOptions:nil];
    _currentCGImageRef = [context createCGImage:_originalCIImage fromRect:[_originalCIImage extent]];
    _picture.image = uiimage;
}

-(void)swipeRecognized:(UISwipeGestureRecognizer *)swipe
{
    CGPoint startLocation;

    if (swipe.state == UIGestureRecognizerStateBegan)
    {
        startLocation = [swipe locationInView:self.view];
    }
    else
    {
        NSLog(@"start loc is %f",startLocation.x);
        CGPoint stopLocation = [swipe locationInView:self.view];
        CGFloat dx = stopLocation.x - startLocation.x;
        CGFloat dy = stopLocation.y - startLocation.y;
        CGFloat distance = sqrt(dx*dx + dy*dy );
        NSLog(@"Distance: %f", distance);
    }
}

当我开始滑动时,我得到一个有效的距离点。然而,当我继续刷卡时,我得到的距离是无限的:

以下是我的代码中的日志打印:

2014-09-23 17:06:57.735 CameraApp [2503:60b] startLocation.x是-0.000000

2014-09-23 17:06:57.738 CameraApp [2503:60b]距离:856.214417

2014-09-23 17:06:57.739 CameraApp [2503:60b] startLocation.x是-413119666577672720342843392.000000

2014-09-23 17:06:57.740 CameraApp [2503:60b]距离:inf

....当我滑动时重复最后2行

您能想到为什么我会变得无限,或者我如何更好地设置我的代码以获得实时刷卡距离数据?

谢谢!

1 个答案:

答案 0 :(得分:1)

startLocation是一个局部变量。在“开始”以外的手势状态中,您不会初始化startLocation导致奇怪的值。你应该把它变成一个实例变量。