user172902
user172902

Reputation: 3581

Pass extra argument for UItapgestureRecognizer with selector

I have two labels, Label1 and Label2. I want to make a single function that prints out which label is tapped by creating UITTapRecognizer for both labels calling the same function with selector that passes an argument. Below is the long way of doing it which is messy but works. If I know how to pass an argument (Int) into the selector, it would be alot cleaner.

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

Is there a way to modify the selector method such that I can do something like

func doubleTapTopComment(label:Int) {
    if label == 1 {
        print("label \(label) double tapped")
}

Upvotes: 5

Views: 4885

Answers (4)

suresh
suresh

Reputation: 53

You can do like this,

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

don't forgot to set tags to labels.

Upvotes: 0

jsondwyer
jsondwyer

Reputation: 437

I believe a UITapGestureRecognizer can only be used for a single view. That said, you can have 2 different UITapGestureRecognizers call the same selector and then access the UITapGestureRecognizer in the function. See the following code:

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)

}

Both UITapGestureRecognizers call the same function:

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

If you try to add one of the UITapGestureRecognizers to both labels, then only the last one added will actually call the function.

Upvotes: 2

danh
danh

Reputation: 62686

Provide both gesture recognizers with the same selector that takes a single parameter. That action method will be passed instance of a UIGestureRecognizer, and happily, that gesture recognizer has a property called view, which is the view to which the gr is attached.

... action: #selector(doubleTapTopComment(_:))

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

Upvotes: 3

Avi
Avi

Reputation: 7552

Short answer: no

The selector is called by the UITapGestureRecognizer, and you have no influence on what parameters it passes.

However, what you can do is query the recognizer's view property to get the same information.

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

Upvotes: 6

Related Questions