UIView touch event in controller

前端 未结 11 1795
梦毁少年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:55

    For swift 4

    @IBOutlet weak var someView: UIView!  
    let gesture = UITapGestureRecognizer(target: self, action:  #selector (self.someAction (_:)))
    self.someView.addGestureRecognizer(gesture)
    
    @objc func someAction(_ sender:UITapGestureRecognizer){
        print("view was clicked")
    }
    

提交回复
热议问题