How to call gesture tap on UIView programmatically in swift

后端 未结 23 1790
情歌与酒
情歌与酒 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
    }
    

提交回复
热议问题