UIView touch event in controller

前端 未结 11 1819
梦毁少年i
梦毁少年i 2020-12-02 06:26

How can I add UIView touchbegin action or touchend action programmatically as Xcode is not providing from Main.storyboard?

11条回答
  •  佛祖请我去吃肉
    2020-12-02 06:57

    Swift 4 / 5:

    let gesture = UITapGestureRecognizer(target: self, action:  #selector(self.checkAction))
    self.myView.addGestureRecognizer(gesture)
    
    @objc func checkAction(sender : UITapGestureRecognizer) {
        // Do what you want
    }
    

    Swift 3:

    let gesture = UITapGestureRecognizer(target: self, action:  #selector(self.checkAction(sender:)))
    self.myView.addGestureRecognizer(gesture)
    
    func checkAction(sender : UITapGestureRecognizer) {
        // Do what you want
    }
    

提交回复
热议问题