我有两个标签,Label1和Label2。我想创建一个单一的函数,通过为这两个标签创建UITTapRecognizer并调用相同的选择器来打印出哪个标签被点击。下面是一种冗长但有效的方式来实现它。如果我知道如何将参数(Int)传递给选择器,那么代码会更加简洁。
有没有一种方法可以修改选择器方法,使我能够做类似这样的事情:
let topCommentLbl1Tap = UITapGestureRecognizer(target: self, action: #selector(DiscoverCell().doubleTapTopComment1))
topCommentLbl1Tap.numberOfTapsRequired = 2
topCommentLbl1.userInteractionEnabled = true
topCommentLbl1.addGestureRecognizer(topCommentLbl1Tap)
let topCommentLbl2Tap = UITapGestureRecognizer(target: self, action: #selector(DiscoverCell().doubleTapTopComment2))
topCommentLbl2Tap.numberOfTapsRequired = 2
topCommentLbl2.userInteractionEnabled = true
topCommentLbl2.addGestureRecognizer(topCommentLbl2Tap)
func doubleTapTopComment1() {
print("Double Tapped Top Comment 1")
}
func doubleTapTopComment2() {
print("Double Tapped Top Comment 2")
}
有没有一种方法可以修改选择器方法,使我能够做类似这样的事情:
func doubleTapTopComment(label:Int) {
if label == 1 {
print("label \(label) double tapped")
}