Search code examples
iosuiimageview

How to move UIImageView using drag event


I would like to ask you about moving UIImageView.

Let’s imagine that an UIImageView is at location X1. If I want to move imv to location X2 using drag event, How can I implement it?

Please give me advice. Thanks for reading.


Solution

  • I have refered to How to drag UIImageView using touches method

    import UIKit
    
    
    class ViewController: UIViewController {
    
        @IBOutlet var imgView: UIImageView!
        var imgMarker:UIImage?
        var location = CGPoint()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
    
            imgMarker = UIImage(named: "marker.png")
            imgView.image = imgMarker
        }
    
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
            if let touch = touches.first {
                location = touch.location(in: self.view)
            imgView.center = location
    
            }
        }
    
    
    }