This question already has answers here:
Can you attach a UIGestureRecognizer to multiple views?
(12个答案)
3年前关闭。
我有2个UIImageView和一个tapGestureRecognizer。
我只能点击第二个UIImageView,即plus1UIImageView。
为什么?
(12个答案)
3年前关闭。
我有2个UIImageView和一个tapGestureRecognizer。
override func viewDidLoad() {
// Do any additional setup after loading the view.
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
cameraUIImageView.isUserInteractionEnabled = true
cameraUIImageView.addGestureRecognizer(tapGestureRecognizer)
plus1UIImageView.isUserInteractionEnabled = true
plus1UIImageView.addGestureRecognizer(tapGestureRecognizer)
//
}
我只能点击第二个UIImageView,即plus1UIImageView。
为什么?
最佳答案
UIGestureRecognizer
必须仅用于单个视图。您正在两个视图中使用同一对象。尝试这个。
override func viewDidLoad() {
// Do any additional setup after loading the view.
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
cameraUIImageView.isUserInteractionEnabled = true
cameraUIImageView.addGestureRecognizer(tapGestureRecognizer)
let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
plus1UIImageView.isUserInteractionEnabled = true
plus1UIImageView.addGestureRecognizer(tapGestureRecognizer2)
}
10-08 06:26