我有两个标签Label1和Label2。我想创建一个函数,通过创建UITTapRecognizer来为两个标签调用与传递参数的选择器调用相同函数的方法来打印哪个标签。下面是这样做的一个很长的路,这是混乱的,但工作。如果我知道如何将一个参数(Int)传递给选择器,它将会变得更清晰。通过选择器传递UItapgestureRecognizer的额外参数
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")
}