[英]Pass extra argument for UItapgestureRecognizer with selector
我有兩個標簽,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")
}
簡短回答:沒有
選擇器由UITapGestureRecognizer
,您對它傳遞的參數沒有影響。
但是,您可以做的是查詢識別器的view
屬性以獲取相同的信息。
func doubleTapComment(recognizer: UIGestureRecognizer) {
if recognizer.view == label1 {
...
}
else if recognizer.view == label2 {
...
}
}
為兩個手勢識別器提供采用單個參數的相同選擇器。 該 action 方法將傳遞UIGestureRecognizer
實例, UIGestureRecognizer
的是,該手勢識別器有一個名為view
的屬性,它是 gr 附加到的視圖。
... action: #selector(doubleTapTopComment(_:))
func doubleTapTopComment(gestureRecognizer: gr) {
// gr.view is the label, so you can say gr.view.text, for example
}
我相信UITapGestureRecognizer
只能用於單個視圖。 也就是說,您可以讓 2 個不同的UITapGestureRecognizer
調用相同的選擇器,然后訪問函數中的UITapGestureRecognizer
。 請參閱以下代碼:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label1 = UILabel()
label1.backgroundColor = UIColor.blueColor()
label1.frame = CGRectMake(20, 20, 100, 100)
label1.tag = 1
label1.userInteractionEnabled = true
self.view.addSubview(label1)
let label2 = UILabel()
label2.backgroundColor = UIColor.orangeColor()
label2.frame = CGRectMake(200, 20, 100, 100)
label2.tag = 2
label2.userInteractionEnabled = true
self.view.addSubview(label2)
let labelOneTap = UITapGestureRecognizer(target: self, action: #selector(ViewController.whichLabelWasTapped(_:)))
let labelTwoTap = UITapGestureRecognizer(target: self, action: #selector(ViewController.whichLabelWasTapped(_:)))
label1.addGestureRecognizer(labelOneTap)
label2.addGestureRecognizer(labelTwoTap)
}
兩個UITapGestureRecognizer
調用相同的函數:
func whichLabelWasTapped(sender : UITapGestureRecognizer) {
//print the tag of the clicked view
print (sender.view!.tag)
}
如果您嘗試將UITapGestureRecognizer
之一添加到兩個標簽,那么只有添加的最后一個才會實際調用該函數。
你可以這樣做,
let topCommentLbl1Tap = UITapGestureRecognizer(target: self, action: #selector(labelTapped(_:)))
let topCommentLbl2Tap = UITapGestureRecognizer(target: self, action: #selector(labelTapped(_:)))
label1.addGestureRecognizer(topCommentLbl1Tap)
label2.addGestureRecognizer(topCommentLbl2Tap)
@objc
func textViewTapped(_ sender: UITapGestureRecognizer) {
if(sender.view.tag == label1.tag) {
print("I am label1")
} else if(sender.view.tag == label2.tag) {
print("I am label2")
}
}
不要忘記將標簽設置為標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.