使用UITapGestureRecognizer传递参数 [英] Pass Parameter with UITapGestureRecognizer
本文介绍了使用UITapGestureRecognizer传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过UITapGestureRecognizer传递参数吗? 我已经看到了针对Objective-C的答案,但找不到迅速的答案
Is there any way i can pass parameters with UITapGestureRecognizer? I've seen this answered for objective-c but couldn't find an answer for swift
test.userInteractionEnabled = true
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped4:"))
// Something like text.myParamater
test.addGestureRecognizer(tapRecognizer)
然后在func imageTapped4(){}下接收myParameter
And then receive myParameter under func imageTapped4(){}
推荐答案
一种方法是将UITapGestureRecognizer子类化,然后设置一个属性,我在下面发布了一个示例.您还可以对发件人进行一些检查,并检查其是否等于某些标签,类,字符串等.
One approach would be to subclass UITapGestureRecognizer and then set a property, I've posted an example below. You could also do some check on the sender and check if equal to some tag, class, string, e.t.c
class ViewController: UIViewController {
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
image.userInteractionEnabled = true;
let tappy = MyTapGesture(target: self, action: #selector(self.tapped(_:)))
image.addGestureRecognizer(tappy)
tappy.title = "val"
}
func tapped(sender : MyTapGesture) {
print(sender.title)
label1.text = sender.title
}
}
class MyTapGesture: UITapGestureRecognizer {
var title = String()
}
关于SO的例子很多,看看吧,祝你好运.
There are lots of examples on SO, have a look, good luck.
这篇关于使用UITapGestureRecognizer传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文