【问题标题】:Pass extra argument for UItapgestureRecognizer with selector使用选择器为 UItapgestureRecognizer 传递额外的参数
【发布时间】:2016-07-11 04:26:07
【问题描述】:

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

【问题讨论】:

    标签: ios swift selector


    【解决方案1】:

    简短回答:没有

    选择器由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
      }
      

      【讨论】:

        【解决方案3】:

        我相信UITapGestureRecognizer 只能用于单个视图。也就是说,您可以让 2 个不同的 UITapGestureRecognizers 调用同一个选择器,然后在函数中访问 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)
        
        }
        

        UITapGestureRecognizers 都调用同一个函数:

        func whichLabelWasTapped(sender : UITapGestureRecognizer) {
            //print the tag of the clicked view
            print (sender.view!.tag)
        }
        

        如果您尝试将UITapGestureRecognizers 之一添加到两个标签,那么只有添加的最后一个会实际调用该函数。

        【讨论】:

          【解决方案4】:

          你可以这样做,

          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")
              }
            }
          

          别忘了给标签设置标签。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-04-15
            • 1970-01-01
            • 2013-07-21
            • 2011-05-24
            • 1970-01-01
            相关资源
            最近更新 更多