How to call gesture tap on UIView programmatically in swift

后端 未结 23 1782
情歌与酒
情歌与酒 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: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")
                }
    

提交回复
热议问题