本文介绍了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滑动长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!