【发布时间】:2016-07-11 04:26:07
【问题描述】:
我有两个标签,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")
}
【问题讨论】: