You can detect touches on a UIImageView
by adding a UITapGestureRecognizer to it.
It's important to note that by default, a UIImageView
has its isUserInteractionEnabled
property set to false, so you must set it explicitly in storyboard or programmatically.
override func viewDidLoad() {
super.viewDidLoad()
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped)))
}
@objc private func imageTapped(_ recognizer: UITapGestureRecognizer) {
print("image tapped")
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…