Here is a complete Pan Gesture example:
We have 2 things – a handle and the gesture itself that uses the handle..
Here is the gesture:
1 2 3 4 5 6 |
// The Pan Gesture func createPanGestureRecognizer(targetView: UIImageView) { var panGesture = UIPanGestureRecognizer(target: self, action: ("handlePanGesture:")) targetView.addGestureRecognizer(panGesture) } |
1 |
("handlePanGesture:")) |
is what points to the Handle ( make sure you don’t misspell this ).
Here is the actual handle:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
// THE HANDLE func handlePanGesture(panGesture: UIPanGestureRecognizer) { // get translation var translation = panGesture.translationInView(view) panGesture.setTranslation(CGPointZero, inView: view) println(translation) //create a new Label and give it the parameters of the old one var label = panGesture.view as UIImageView label.center = CGPoint(x: label.center.x+translation.x, y: label.center.y+translation.y) label.multipleTouchEnabled = true label.userInteractionEnabled = true if panGesture.state == UIGestureRecognizerState.Began { //add something you want to happen when the Label Panning has started } if panGesture.state == UIGestureRecognizerState.Ended { //add something you want to happen when the Label Panning has ended } if panGesture.state == UIGestureRecognizerState.Changed { //add something you want to happen when the Label Panning has been change ( during the moving/panning ) } else { // or something when its not moving } } |