UISwipeGestureRecognizer刷卡长度
任何想法,如果有一种方法来获得一个滑动手势的长度或触摸,以便我可以计算距离?
从滑动手势开始距离是不可能的,因为SwipeGesture会在手势结束时触发一次您可以准确访问位置的方法。
也许你想使用UIPanGestureRecognizer。
如果可以使用平移手势,则可以保存平底锅的起点,如果平底锅已经结束,则计算距离。
- (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); } }
在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 } } }
希望能帮助你所有的Swift先驱者
你只能做一个标准的方法:记住touchBegin的触点并比较touchEnd的点。
对于我们这些使用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); } }
func swipeAction(gesture: UIPanGestureRecognizer) { let transition = sqrt(pow(gesture.translation(in: view).x, 2) + pow(gesture.translation(in: view).y, 2)) }