UISwipeGestureRecognizer 滑动长度 [英] UISwipeGestureRecognizer Swipe length

查看:31
本文介绍了UISwipeGestureRecognizer 滑动长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

知道是否有办法获得滑动手势或触摸的长度以便我可以计算距离吗?

Any idea if there is a way to get the length of a swipe gesture or the touches so that i can calculate the distance?

推荐答案

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

It's impossible to get a distance from a swipe gesture, because the SwipeGesture triggers the method where you could access the location exactly one time, when the gesture has ended.
Maybe you want to use a UIPanGestureRecognizer.

如果您可以使用平移手势,您将保存平移的起点,如果平移已经结束,则计算距离.

If it possible for you to use pan gesture you would save the starting point of the pan, and if the pan has ended calculate the distance.

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

这篇关于UISwipeGestureRecognizer 滑动长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆