iOS adding tapGesture to multiple Views

前端 未结 6 1041
无人及你
无人及你 2020-12-31 03:11

I have multiple views defined in my main view. I want to add single tap gesture to all these views. Below is the code I have written, but this registers a tap gesture to the

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 03:50

    // Add tap gesture in swift 4

        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
    @objc func handleTap(_ sender: UITapGestureRecognizer) {
        if sender.view == view1 {
           // do something
        }
        else{
           // do next thing
        }
    }
    

    //USE in view did load: tapGestures(view: view1) tapGestures(view: view2) tapGestures(view: view3)

提交回复
热议问题