为选择器传递UItapgestureRecognizer的额外参数

时间:2016-07-11 04:26:07

标签: ios swift 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")
}

4 个答案:

答案 0 :(得分:2)

简答:没有

选择器由UITapGestureRecognizer调用,你对它传递的参数没有影响。

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

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

答案 1 :(得分: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 :(得分:1)

我相信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添加到两个标签中,则只添加最后一个标签实际上会调用该函数。

答案 3 :(得分: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")
    }
  }

不要忘记将标签设置为标签。