Pass parameter with UITapGestureRecognizer

Is there a way to pass parameters using a UITapGestureRecognizer? I saw how this was responsible for objective-c, but could not find an answer for quick

test.userInteractionEnabled = true let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped4:")) // Something like text.myParamater test.addGestureRecognizer(tapRecognizer) 

And then get myParameter under func imageTapped4 () {}

+11
source share
4 answers

One approach is to subclass UITapGestureRecognizer and then set the property. I posted an example below. You can also check the sender and check if it is equal to some tag, class, string, etc.

 class ViewController: UIViewController { @IBOutlet weak var label1: UILabel! @IBOutlet weak var image: UIImageView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. image.userInteractionEnabled = true; let tappy = MyTapGesture(target: self, action: #selector(self.tapped(_:))) image.addGestureRecognizer(tappy) tappy.title = "val" } func tapped(sender : MyTapGesture) { print(sender.title) label1.text = sender.title } } class MyTapGesture: UITapGestureRecognizer { var title = String() } 

There are many examples on SO, look, good luck.

+28
source

For Swift 4

In Viewdidload

 let label = UILabel(frame: CGRect(x: 0, y: h, width: Int(self.phoneNumberView.bounds.width), height: 30)) label.textColor = primaryColor label.numberOfLines = 0 label.font = title3Font label.lineBreakMode = .byWordWrapping label.attributedText = fullString let phoneCall = MyTapGesture(target: self, action: #selector(self.openCall)) phoneCall.phoneNumber = "\(res)" label.isUserInteractionEnabled = true label.addGestureRecognizer(phoneCall) 

Function as

 @objc func openCall(sender : MyTapGesture) { let number = sender.phoneNumber print(number) } 

Write a class like

 class MyTapGesture: UITapGestureRecognizer { var phoneNumber = String() } 

Follow the step properly and make changes according to your variable, button, shortcut. It works correctly

+4
source

The message sent to the touch gestures is basically similar to the location of the tapping element found in the view.

Suppose we have added a touch gesture in the self.view controller view.

  override func viewDidLoad() { super.viewDidLoad() let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.printGesPostion)) self.view.addGestureRecognizer(tapGesture) // Do any additional setup after loading the view, typically from a nib. } 

We can implement the printGesPosition method as

  func printGesPostion(ges:UITapGestureRecognizer) { print(ges.locationInView(self.view)) let subView = self.view.hitTest(ges.locationInView(self.view), withEvent: nil) if subView?.isKindOfClass(UILabel) == true { print( (subView as! UILabel).text! ) } } 

In this case, the method detects the position of the tap gestures; if it occurs on a UILabel , it will print label.text

Remember to set label.userInteraction = true , either in ViewDidLoad or just in the storyboard.

-1
source

The best way is to determine the parameter when func imageTapped64 . You can get the parameters using the view (look at @Developer Sheldon's answer) or in many other ways.

-1
source

All Articles