UIGestureRecognizer on UIImageView

前端 未结 8 1077
南旧
南旧 2020-11-28 01:32

I have a UIImageView, which I want to be able to resize and rotate etc.

Can a UIGestureRecognizer be added to the UIImageView?

相关标签:
8条回答
  • 2020-11-28 02:36

    Swift 4.2

    myImageView.isUserInteractionEnabled = true
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
    tapGestureRecognizer.numberOfTapsRequired = 1
    myImageView.addGestureRecognizer(tapGestureRecognizer)
    

    and when tapped:

    @objc func imageTapped(_ sender: UITapGestureRecognizer) {
       // do something when image tapped
       print("image tapped")
    }
    
    0 讨论(0)
  • 2020-11-28 02:38

    SWIFT 3 Example

    override func viewDidLoad() {
    
        self.backgroundImageView.addGestureRecognizer(
            UITapGestureRecognizer.init(target: self, action:#selector(didTapImageview(_:)))
        )
    
        self.backgroundImageView.isUserInteractionEnabled = true
    }
    
    func didTapImageview(_ sender: Any) {
        // do something
    }
    

    No gesture recongnizer delegates or other implementations where necessary.

    0 讨论(0)
提交回复
热议问题