How to implement multiple PanGestures (Draggable views)?

后端 未结 3 873
后悔当初
后悔当初 2021-01-27 06:39

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 Vie         


        
3条回答
  •  盖世英雄少女心
    2021-01-27 07:19

    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:

    import UIKit
    
    class ViewController: UIViewController {
    
    
    @IBOutlet weak var boxToMove1: UIView!
    @IBOutlet weak var boxToMove2: UIView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    @IBAction func handlePan(objectToMove:UIPanGestureRecognizer) {
    
        let translation = objectToMove.translationInView(self.view)
        objectToMove.view!.center = CGPoint(x: objectToMove.view!.center.x + translation.x, y: objectToMove.view!.center.y + translation.y)
        objectToMove.setTranslation(CGPointZero, inView: self.view)
    
        println("\(objectToMove.view!.tag)") //Verify correct object received
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    }
    

    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?

提交回复
热议问题