Francis Francis - 2 months ago 39
iOS Question

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/library/prerelease/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/index.html

Answer

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)
Comments