使用UISwipeGestureRecognizer查找长度

时间:2015-05-23 16:59:03

标签: ios swift

我正在使用此代码来检测向上滑动手势:

let swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("upSwiped"))
swipeUp.direction = UISwipeGestureRecognizerDirection.Up
self.matn.addGestureRecognizer(swipeUp)

func upSwiped()
{
     println("up swiped")
}

是否可以使用UISwipeGestureRecognizer找到长度或手势的起点和手势来计算长度?

1 个答案:

答案 0 :(得分:0)

从滑动手势中获取距离是不可能的,因为SwipeGesture触发了一种方法,当手势结束时,您可以准确地访问该位置一次。 也许你想使用UIPanGestureRecognizer。

如果您可以使用平移手势,则可以保存平移的起点,如果平移已结束,则计算距离:UISwipeGestureRecognizer Swipe length

- (void)panGesture:(UIPanGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        startLocation = [sender locationInView:self.view];
    }
    else if (sender.state == UIGestureRecognizerStateEnded) {
        CGPoint stopLocation = [sender 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);
    }
}