How to call gesture tap on UIView programmatically in swift

后端 未结 23 1674
情歌与酒
情歌与酒 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:15

    I wanted to specify two points which kept causing me problems.

    • I was creating the Gesture Recognizer on init and storing it in a let property. Apparently, adding this gesture recog to the view does not work. May be self object passed to the gesture recognizer during init, is not properly configured.
    • The gesture recognizer should not be added to views with zero frames. I create all my views with zero frame and then resize them using autolayout. The gesture recognizers have to be added AFTER the views have been resized by the autolayout engine. So I add the gesture recognizer in viewDidAppear and they work.
    0 讨论(0)
  • 2020-11-28 19:18

    This is how it works in Swift 3:

    @IBOutlet var myView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let tap = UITapGestureRecognizer(target: self, action:#selector(handleTap))
    
        myView.addGestureRecognizer(tap)
    }
    
    func handleTap() {
        print("tapped")
    }
    
    0 讨论(0)
  • 2020-11-28 19:19

    For Swift 4:

    let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
    
    view.addGestureRecognizer(tap)
    
    view.isUserInteractionEnabled = true
    
    self.view.addSubview(view)
    
    // function which is triggered when handleTap is called
    @objc func handleTap(_ sender: UITapGestureRecognizer) {
        print("Hello World")
    }
    

    In Swift 4, you need to explicitly indicate that the triggered function is callable from Objective-C, so you need to add @objc too your handleTap function.

    See @Ali Beadle 's answer here: Swift 4 add gesture: override vs @objc

    0 讨论(0)
  • 2020-11-28 19:20
        let tap = UITapGestureRecognizer(target: self, action: Selector("handleFrontTap:"))
        frontView.addGestureRecognizer(tap)
    
    // Make sure this is not private
    func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
        print("tap working")
    }
    
    0 讨论(0)
  • 2020-11-28 19:24

    try the following extension

        extension UIView {
    
        func  addTapGesture(action : @escaping ()->Void ){
            let tap = MyTapGestureRecognizer(target: self , action: #selector(self.handleTap(_:)))
            tap.action = action
            tap.numberOfTapsRequired = 1
    
            self.addGestureRecognizer(tap)
            self.isUserInteractionEnabled = true
    
        }
        @objc func handleTap(_ sender: MyTapGestureRecognizer) {
            sender.action!()
        }
    }
    
    class MyTapGestureRecognizer: UITapGestureRecognizer {
        var action : (()->Void)? = nil
    }
    

    and then use it :

    submitBtn.addTapGesture {
         //your code
    }
    

    you can even use it for cell

    cell.addTapGesture {
         //your code
    }
    
    0 讨论(0)
  • 2020-11-28 19:26

    Swift 4

    First, create an object of UITapGestureRecognizer

    var tapGesture = UITapGestureRecognizer()
    

    The second step is to initialise UITapGestureReconizer. Enable the user interaction, then add it.

    override func viewDidLoad() {
            super.viewDidLoad()
        tapGesture = UITapGestureRecognizer(target: self, action: #selector(YourViewController.myviewTapped(_:)))
                infosView.isUserInteractionEnabled = true
                infosView.addGestureRecognizer(tapGesture)
    view.addSubview(infosView)
    }
    

    Third, create a method

    @objc func myviewTapped(_ recognizer: UIGestureRecognizer) {
                    print("button is tapped")
                }
    
    0 讨论(0)
提交回复
热议问题