0

I'm using this code to detect swipe-up gesture:

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

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

Is it possible to find length or start and point of gesture to calculate length using UISwipeGestureRecognizer?

Maysam
  • 7,246
  • 13
  • 68
  • 106

1 Answers1

0

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 as written: 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);
    }
}
Community
  • 1
  • 1