简体   繁体   中英

add tapGestureRecognizer to UIImageView in UIView and call method by protocol

I want to make touch recognized on an UIImageView(orange) in an UIView(blue). And hopefully, handle the touch event in a delegate method, which will be used in other classes. However, I can't make touch recognized with below code. How can I implement to recognize touch only in the ImageView(orange).

在此处输入图片说明

code:

import UIKit

protocol CustomViewDelegate {
    func imageViewTapped()
}

class CustomView: UIView {

    var delegate: CustomViewDelegate?
    var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.blue
        self.imageView.backgroundColor = UIColor.orange
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapped))
        imageView.addGestureRecognizer(tapGestureRecognizer)
        self.addSubview(imageView)
    }

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func tapped() {
        print("tapped method called")
        delegate?.imageViewTapped()

    }
}

class ViewController: UIViewController, CustomViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let view = CustomView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
        view.delegate = self
        self.view.addSubview(view)
    }

    func imageViewTapped() {
        print("come to view controller")
    }

}

UIImageView的默认userinteraction为false,因此在CustomView init中启用imageView userinteraction。

imageView.isUserInteractionEnabled = true

Add Different UITapGestureRecognizer to different views and handle as per your requirement . or add tag and access like this

 func tapOneAct(sender: UITapGestureRecognizer){

    if let tag = sender.view?.tag {
        switch tag {
        case 1:
            println("First View Tapped")
        case 2:
            println("Second View Tapped")
        default:
            println("Nothing Tapped")
        }
    }

}

enable isUserInteractionEnabled property on UIImageView as it defaults to false

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(CustomView.tapped(_:))) 
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
self.addSubview(imageView)

in tapGesture method

func tapgesture(_ sender: UITapGestureRecognizer){
     if sender.state == .ended {
        let touchLocation: CGPoint = sender.location(in: sender.view?.superview)
        if imageView.frame.contains(touchLocation) {
            print("tapped method called")
            delegate?.imageViewTapped()
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM