UISwipeGestureRecognizer Swipe length

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

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?

回答1:

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


回答2:

In Swift

 override func viewDidLoad() {     super.viewDidLoad()      // add your pan recognizer to your desired view     let panRecognizer = UIPanGestureRecognizer(target: self, action: Selector("panedView:"))     self.view.addGestureRecognizer(panRecognizer)  }   func panedView(sender:UIPanGestureRecognizer){     if (sender.state == UIGestureRecognizerState.Began) {         startLocation = sender.locationInView(self.view);     }     else if (sender.state == UIGestureRecognizerState.Ended) {         let stopLocation = sender.locationInView(self.view);         let dx = stopLocation.x - startLocation.x;         let dy = stopLocation.y - startLocation.y;         let distance = sqrt(dx*dx + dy*dy );         NSLog("Distance: %f", distance);          if distance > 400 {             //do what you want to do          }      }  } 

Hope that helps all you Swift pioneers



回答3:

You can only do it a standard way: remember the touch point of touchBegin and compare the point from touchEnd.



回答4:

For those of us using Xamarin:

void panGesture(UIPanGestureRecognizer gestureRecognizer) {     if (gestureRecognizer.State == UIGestureRecognizerState.Began) {         startLocation = gestureRecognizer.TranslationInView (view)     } else if (gestureRecognizer.State == UIGestureRecognizerState.Ended) {         PointF stopLocation = gestureRecognizer.TranslationInView (view);         float dX = stopLocation.X - startLocation.X;         float dY = stopLocation.Y - startLocation.Y;         float distance = Math.Sqrt(dX * dX + dY * dY);         System.Console.WriteLine("Distance: {0}", distance);     } } 


回答5:

func swipeAction(gesture: UIPanGestureRecognizer) {     let transition = sqrt(pow(gesture.translation(in: view).x, 2)                      + pow(gesture.translation(in: view).y, 2)) } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!