我想用TapGestureRecognizer
实现selector
,下面是我将tapGestureRecognizer添加到imageView
let tapFirstGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(assignImage(_:)))
firstImageView.isUserInteractionEnabled = true
firstImageView.tag = 1
firstImageView.addGestureRecognizer(tapFirstGestureRecognizer)
这是动作方法
func assignImage(_ sender: UIImageView){
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
imageViewTag = sender.tag
present(imagePicker, animated: true, completion: nil)
}
编译器一直在说
[UITapGestureRecognizer tag]:无法识别的选择器发送到实例0x6080001a7700'
答案 0 :(得分:9)
问题在于您的方法声明以及为tag
的对象分配UIGestureRecognizer
。像这样更改方法声明。
func assignImage(_ sender: UITapGestureRecognizer)
或
func assignImage(_ sender: UIGestureRecognizer)
修改:使用UITapGestureRecognizer
访问imageView对象。
func assignImage(_ sender: UITapGestureRecognizer) {
if let imageView = sender.view as? UIImageView {
}
}
注意:您需要tag
与imageView
一起设置UITapGestureRecognizer