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

5
我有两个标签,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")
}
4个回答

6

简短回答:不可以

选择器由UITapGestureRecognizer调用,您无法影响其传递的参数。

但是,您可以查询识别器的view属性以获取相同的信息。

func doubleTapComment(recognizer: UIGestureRecognizer) {
    if recognizer.view == label1 {
        ...
    }
    else if recognizer.view == label2 {
        ...
    }
}

2
提供相同的选择器给两个手势识别器,该选择器需要一个参数。那个操作方法将被传递一个 UIGestureRecognizer 的实例,幸运的是,该手势识别器有一个叫做 view 的属性,它是与 gr 相关联的视图。
... action: #selector(doubleTapTopComment(_:))

func doubleTapTopComment(gestureRecognizer: gr) {
    // gr.view is the label, so you can say gr.view.text, for example
}

2

我相信UITapGestureRecognizer只能用于单个视图。也就是说,您可以有两个不同的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添加到两个标签中,那么只有最后一个添加的标签会实际调用该函数。


0
你可以这样做,
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")
    }
  }

不要忘记给标签设置标记。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接