问题描述
我想用 selector
实现 TapGestureRecognizer
,下面是我将 tapGestureRecognizer 添加到我的 imageView
I want to implement TapGestureRecognizer
with the selector
, below is the code where I added tapGestureRecognizer to my 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'
推荐答案
问题在于您的方法声明以及将 tag
分配给 UIGestureRecognizer
的对象.像这样更改您的方法声明.
The problem is with your method declaration and with assigning tag
to the object of UIGestureRecognizer
. Change your method declaration like this.
func assignImage(_ sender: UITapGestureRecognizer)
或者
func assignImage(_ sender: UIGestureRecognizer)
使用 UITapGestureRecognizer
访问 imageView 对象.
To access imageView object with UITapGestureRecognizer
.
func assignImage(_ sender: UITapGestureRecognizer) {
if let imageView = sender.view as? UIImageView {
}
}
注意:你需要用你的imageView
设置tag
而不是你UITapGestureRecognizer
Note: You need to set tag
with you imageView
not with you UITapGestureRecognizer
这篇关于无法识别的选择器发送到实例 Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!