使用选择器为 UItapgestureRecognizer 传递额外参数

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 {
        ...
    }
}

为两个手势识别器提供带有单个参数的相同选择器。该动作方法将传递 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")
    }
  }

别忘了给标签设置标签。