How to call gesture tap on UIView programmatically in swift

后端 未结 23 1696
情歌与酒
情歌与酒 2020-11-28 18:50

I have a UIView and and I have added tap gesture to it:

let tap = UITapGestureRecognizer(target: self, action: Selector(\"handleTap:\"))
tap.delegate = self         


        
相关标签:
23条回答
  • 2020-11-28 19:02

    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
    }
    
    0 讨论(0)
  • 2020-11-28 19:02

    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)
    }
    
    0 讨论(0)
  • 2020-11-28 19:03

    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()
    
    0 讨论(0)
  • 2020-11-28 19:03

    Swift 4

    let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchTapped(_:)))
        self.view.addGestureRecognizer(tap)
    
    @objc func touchTapped(_ sender: UITapGestureRecognizer) {
    }
    
    0 讨论(0)
  • 2020-11-28 19:03

    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")
            }
        }
    
    0 讨论(0)
  • 2020-11-28 19:04

    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)
    
    0 讨论(0)
提交回复
热议问题