I have a UIView and and I have added tap gesture to it:
let tap = UITapGestureRecognizer(target: self, action: Selector(\"handleTap:\"))
tap.delegate = self
You need to initialize UITapGestureRecognizer with a target and action, like so:
let tap = UITapGestureRecognizer(target: self, action: "handleTap:")
tap.delegate = self
myView.addGestureRecognizer(tap)
Then, you should implement the handler, which will be called each time when a tap event occurs:
func handleTap(sender: UITapGestureRecognizer) {
// handling code
}
Inside ViewDidLoad
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
self.imgMainAdView.isUserInteractionEnabled = true
self.imgMainAdView.addGestureRecognizer(tapGestureRecognizer)
//MARK: - Image Tap Method -
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
print("Tapped")
if let url = URL(string: self.strMAinAdvLink)
{
UIApplication.shared.open(url, options: [:])
}
}
Calling Purpose
@IBAction func btnCall1Action(_ sender: Any)
{
let text = self.strPhoneNumber1!
let test = String(text.filter { !" -()".contains($0) })
UIApplication.shared.openURL(NSURL(string: "tel://\(test)")! as URL)
}
Mail Purpose
MFMailComposeViewControllerDelegate
@IBAction func btnMailAction(_ sender: Any)
{
let strEmail = SAFESTRING(str: (self.dictEventDetails?.value(forKeyPath: "Email.value_text.email") as! String))
if !MFMailComposeViewController.canSendMail()
{
AppDelegate.sharedInstance().showAlertAction(strTitle: "OK", strMessage: "Mail services are not available") { (success) in
}
return
}
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
composeVC.setToRecipients([strEmail])
composeVC.setSubject("")
composeVC.setMessageBody("", isHTML: false)
self.present(composeVC, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
{
controller.dismiss(animated: true, completion: nil)
}
You need to initialize UITapGestureRecognizer
with a target and action, like so:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)
Then, you should implement the handler, which will be called each time when a tap event occurs:
@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
// handling code
}
So now calling your tap gesture recognizer event handler is as easy as calling a method:
handleTap()
Swift 4
let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchTapped(_:)))
self.view.addGestureRecognizer(tap)
@objc func touchTapped(_ sender: UITapGestureRecognizer) {
}
Swift 5.1 Example for three view
Step:1 -> Add storyboard view and add outlet viewController UIView
@IBOutlet var firstView: UIView!
@IBOutlet var secondView: UIView!
@IBOutlet var thirdView: UIView!
Step:2 -> Add storyBoard view Tag
Step:3 -> Add gesture
override func viewDidLoad() {
super.viewDidLoad()
firstView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tap(_:))))
firstView.isUserInteractionEnabled = true
secondView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tap(_:))))
secondView.isUserInteractionEnabled = true
thirdView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tap(_:))))
thirdView.isUserInteractionEnabled = true
}
Step:4 -> select view
@objc func tap(_ gestureRecognizer: UITapGestureRecognizer) {
let tag = gestureRecognizer.view?.tag
switch tag! {
case 1 :
print("select first view")
case 2 :
print("select second view")
case 3 :
print("select third view")
default:
print("default")
}
}
Complete answer for Swift 4
Step 1: create an outlet for the view
@IBOutlet weak var rightViewOutlet: UIView!
Step 2: define a tap gesture
var tapGesture = UITapGestureRecognizer()
Step 3: create ObjC function (called when view tapped)
@objc func rightViewTapped(_ recognizer: UIGestureRecognizer) {
print("Right button is tapped")
}
Step 4: add the following within viewDidLoad()
let rightTap = UITapGestureRecognizer(target: self, action: #selector(ViewController.rightViewTapped(_:)))
rightViewOutlet.addGestureRecognizer(rightTap)