我有兩個標籤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")
}