如何使用拖动事件移动UIImageView

问题描述 投票:0回答:1

我想问您有关移动UIImageView的问题。

让我们假设UIImageView位于位置X1。如果我想使用拖动事件将imv移至位置X2,该如何实现?

请给我建议。谢谢阅读。

ios uiimageview
1个回答
0
投票

我指的是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

        }
    }


}


© www.soinside.com 2019 - 2024. All rights reserved.