Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift UIGestureRecogniser follow finger

I'm making an iOS8 app using Swift. I'd like the user to be able to use gestures to reveal certain parts of the interface. So for example, the user slides their finger up and the view they slid their finger up moves out of the way, following their finger to reveal another view underneath.

What I'd like is a gesture to give a result similar to the notification box that you can pull down from the top of the screen. I've been looking at the documentation and I can't seem to find a suitable gesture.

I saw one called UISwipeGestureRecogniser, but the only problem is, it doesn't follow your finger, it simply runs a function when I slide my finger up / down.

Here's the documentation page: https://developer.apple.com/documentation/uikit/uigesturerecognizer

like image 374
Francis Avatar asked Aug 26 '14 10:08

Francis


People also ask

What is a pan gesture?

A pan gesture occurs any time a person moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.

How do you use UILongPressGestureRecognizer?

UILongPressGestureRecognizer is a concrete subclass of UIGestureRecognizer . The user must press one or more fingers on a view and hold them there for a minimum period of time before the action triggers. While down, the userʼs fingers canʼt move more than a specified distance or the gesture fails.

What is UITapGestureRecognizer Swift?

UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.


1 Answers

You're looking for the UIPanGestureRecognizer. You'll find the Apple Documentation here.

Here's a sample handler that will move a view with your finger. In Interface Builder, add a UIPanGestureRecognizer to a view that you want to be able to drag. Set the delegate to your ViewController. Set the action to this action:

Swift 2.X:

@IBAction func handlePan(gestureRecognizer: UIPanGestureRecognizer) {     if gestureRecognizer.state == .Began || gestureRecognizer.state == .Changed {          let translation = gestureRecognizer.translationInView(self.view)           // note: 'view' is optional and need to be unwrapped         gestureRecognizer.view!.center = CGPointMake(gestureRecognizer.view!.center.x + translation.x, gestureRecognizer.view!.center.y + translation.y)           gestureRecognizer.setTranslation(CGPointMake(0,0), inView: self.view)       }   }   

Swift 3:

@IBAction func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {     if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {          let translation = gestureRecognizer.translation(in: self.view)         // note: 'view' is optional and need to be unwrapped         gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)         gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)     } } 

Of course, you can add the UIPanGestureRecognizer programmatically:

In viewDidLoad for your ViewController, create the recognizer and add it to the view you want to be able to drag:

    let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))     self.someDraggableView.addGestureRecognizer(gestureRecognizer) 
like image 78
vacawama Avatar answered Sep 20 '22 03:09

vacawama