Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draggable UIButton/Elements in Swift?

Tags:

ios

swift

this is my first question! I was just wondering, in Swift (specifically Swift 2, although that may go without saying!), how you create a button that the user can drag around. So for example, if it is a UIButton, the user can tap and hold it, and when they move their finger, the UIButton moves with it, and when they release it, it remains in the position that the user left it. Potentially there could be a snapping system but this is unimportant for now.

I've searched StackOverflow and found some quite interesting things, however it's all for Objective-C, and although Swift is pretty similar in some respects, I can't figure out in the slightest as to how to implement this in Swift.

It would be massively appreciated for a project that I am working on!

Thank you very much!

like image 658
caluga Avatar asked Mar 14 '16 22:03

caluga


3 Answers

You can implement UIPanGestureRecognizer on your UIButton.

Wherever you create your button (viewDidLoad if using outlets):

let pan = UIPanGestureRecognizer(target: self, action: "panButton:")
button.addGestureRecognizer(pan)

This creates a new pan gesture recognizer and adds it to the button. Now, you'll want to implement the pan's action. First, you need to store the center of the button to be able to reset it when you finish panning. Add this as a view controller property:

var buttonCenter = CGPointZero

Then you implement the pan action. Note that you can use gesture recognizer states to determine when the pan starts and ends:

func panButton(pan: UIPanGestureRecognizer) {

    if pan.state == .Began {
        buttonCenter = button.center // store old button center
    } else if pan.state == .Ended || pan.state == .Failed || pan.state == .Cancelled {
        button.center = buttonCenter // restore button center
    } else {
        let location = pan.locationInView(view) // get pan location
        button.center = location // set button to where finger is
    }

}
like image 188
tktsubota Avatar answered Nov 14 '22 13:11

tktsubota


Swift 4 & 5 Version of accepted answer:

var buttonCenter: CGPoint = .zero

viewDidLoad() {
    super.viewDidLoad()

    let pan = UIPanGestureRecognizer(target: self, action: #selector(YourViewController.panButton(pan:)))
    button.addGestureRecognizer(pan)
}

@objc func panButton(pan: UIPanGestureRecognizer) {
    if pan.state == .began {
        buttonCenter = button.center // store old button center
    } else if pan.state == .ended || pan.state == .failed || pan.state == .cancelled {
        button.center = buttonCenter // restore button center
    } else {
        let location = pan.location(in: view) // get pan location
        button.center = location // set button to where finger is
    }
}
like image 33
Dennis Stücken Avatar answered Nov 14 '22 11:11

Dennis Stücken


Basically, you want to implement a touch gesture recognizer and set the button's center to the center of your press when you tap/move said button.

Here's how you'll want to do that: https://stackoverflow.com/a/31487087/5700898

Also, really cool idea!

like image 42
owlswipe Avatar answered Nov 14 '22 12:11

owlswipe