Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISwipeGestureRecognizer Swipe length

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?

like image 320
Tomo Avatar asked Jan 28 '11 13:01

Tomo


People also ask

What is the swipe gesture?

You can swipe left or right from the Apps screen of your Android phone or tablet to see more pages of apps. When you're using a web browser or an app such as Facebook or WhatsApp, you can swipe up to move down the page, and swipe back down to return to where you started.

What subclass of the UIGestureRecognizer we will use in order to interpret swiping in one or more directions?

Overview. UISwipeGestureRecognizer is a concrete subclass of UIGestureRecognizer . UISwipeGestureRecognizer recognizes a swipe when the user moves the specified number of touches ( numberOfTouchesRequired ) in an allowable direction ( direction ) far enough to create a swipe.

How do you use swipe gestures in Swift 4?

A swipe gesture recognizer detects swipes in one of four directions, up , down , left , and right . We set the direction property of the swipe gesture recognizer to down . If the user swipes from the top of the blue view to the bottom of the blue view, the swipe gesture recognizer invokes the didSwipe(_:)


2 Answers

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);
    }
}
like image 106
Matthias Bauch Avatar answered Oct 17 '22 10:10

Matthias Bauch


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)

}

   @objc func panedView(sender:UIPanGestureRecognizer){
        var startLocation = CGPoint()
        //UIGestureRecognizerState has been renamed to UIGestureRecognizer.State in Swift 4
        if (sender.state == UIGestureRecognizer.State.began) {
            startLocation = sender.location(in: self.view)
        }
        else if (sender.state == UIGestureRecognizer.State.ended) {
            let stopLocation = sender.location(in: 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

like image 22
Chris Klingler Avatar answered Oct 17 '22 11:10

Chris Klingler