I want to have several objects that I can drag and drop.
Here's my code for moving one object (with lot of help from @vacawama):
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var panView: UIView!
@IBOutlet weak var panViewCenterX: NSLayoutConstraint!
@IBOutlet weak var panViewCenterY: NSLayoutConstraint!
let panRec = UIPanGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
panRec.addTarget(self, action: "draggedView:")
panView.addGestureRecognizer(panRec)
// Do any additional setup after loading the view, typically from a nib.
}
func draggedView(sender:UIPanGestureRecognizer){
self.view.bringSubviewToFront(sender.view!)
var translation = sender.translationInView(self.view)
panViewCenterY.constant += translation.y
panViewCenterX.constant += translation.x
sender.setTranslation(CGPointZero, inView: self.view)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I want to have more objects in my project:
@IBOutlet weak var panView2: UIView!
@IBOutlet weak var panView3: UIView!
@IBOutlet weak var panView4: UIView!
...
So, what is the easiest way to implement this in such way that I can have multiple panView
which I can move independently around (one at the time)?
Finally:
Found this example which explained it in a easy way: http://www.raywenderlich.com/76020/using-uigesturerecognizer-with-swift-tutorial
So, now multiple Pan's are working..
Here's the code:
I linked up the "Pan Gesture Recognizer" from the "Object library" to each
boxToMove
. Then I linked the "Pan Gesture Recognizer" (both) to my@IBAction func handlePan
...This is now working!!! Finally I understand the way this Gesture Recognizer is working!
What I'm still trying to figure out is how to do all this linking programmatically.. Any idea?
If you are using Auto Layout, you shouldn't just move a view by altering its center. Anything that causes Auto Layout to run will move your view back to its original position.
Here is an implementation of a new class called
DraggableView
that works with Auto Layout to provide views that can be dragged. It creates constraints in code for the width, height, X position, and Y position of the view. It uses its ownUIPanGestureRecognizer
to allow the view to be dragged.DraggableView.swift can be dropped into any project that needs draggable views. Take a look at ViewController.swift below to see how easy it is to use.
DraggableView.swift
ViewController.swift
UPDATE:
Here is a version of DraggableView.swift that supports images as a subview.
The way a gesture recognizer works is that you attach it to a view. So if you have multiple views, and you want them to be draggable in the same way, attach a different pan gesture recognizer to each one.
Now let's say you want them all to have the same action handler. No problem. In the action handler, you receive the current gesture recognizer as a parameter. It has a
view
property; that's the view it is attached to! So now you know that that's the view you're moving.