How to call gesture tap on UIView programmatically in swift

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

    For anyone who is looking for Swift 3 solution

    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
     func handleTap(_ sender: UITapGestureRecognizer) {
         print("Hello World")
      }
    

提交回复
热议问题