Reputation: 387
Is there any way i can pass parameters with UITapGestureRecognizer? I've seen this answered for objective-c but couldn't find an answer for swift
test.userInteractionEnabled = true
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped4:"))
// Something like text.myParamater
test.addGestureRecognizer(tapRecognizer)
And then receive myParameter under func imageTapped4(){}
Upvotes: 25
Views: 24536
Reputation: 1559
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 = phoneNumber
label.isUserInteractionEnabled = true
label.addGestureRecognizer(phoneCall)
The openCall function:
@objc func openCall(sender : MyTapGesture) {
if let number = sender.phoneNumber {
print(number)
}
}
The custom Gesture Recognizer class:
class MyTapGesture: UITapGestureRecognizer {
var phoneNumber: String?
}
Upvotes: 22
Reputation: 2895
There is a NSString* name property for debugging available for misuse:
https://developer.apple.com/documentation/uikit/uigesturerecognizer/2890966-name?language=objc
- (void)handleTapOnLink:(UITapGestureRecognizer *)sender
{
NSString *url = sender.name;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
}
Upvotes: -1
Reputation: 335
Without subclassing, you can also add the parameter in the view layer (where the gesture is attached):
let tappy = UITapGestureRecognizer(target: self, action: #selector(self.tapped(_:)))
image.addGestureRecognizer(tappy)
image.layer.setValue(title, forKey: "anyKeyName")
Later in the handler
@objc private func tapped(_ sender: UIGestureRecognizer) {
guard let title = sender.view?.layer.value(forKey: "anyKeyName") as? String else { return }
// Do something with the title
}
Upvotes: 0
Reputation: 1790
One approach would be to subclass UITapGestureRecognizer and then set a property, I've posted an example below. You could also do some check on the sender and check if equal to some tag, class, string, e.t.c
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 lots of examples on SO, have a look, good luck.
Upvotes: 61
Reputation: 2215
The best way is to determind the parameter when the func imageTapped64
is fired. You can get you params via the view (take a look to @Developer Sheldon answer)
or in many other ways.
Upvotes: -1