Pass extra argument for UItapgestureRecognizer with selector

后端 未结 4 1461
日久生厌
日久生厌 2020-12-11 22:33

I have two labels, Label1 and Label2. I want to make a single function that prints out which label is tapped by creating UITTapRecognizer for both labels calling the same fu

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 23:13

    Provide both gesture recognizers with the same selector that takes a single parameter. That action method will be passed instance of a UIGestureRecognizer, and happily, that gesture recognizer has a property called view, which is the view to which the gr is attached.

    ... action: #selector(doubleTapTopComment(_:))
    
    func doubleTapTopComment(gestureRecognizer: gr) {
        // gr.view is the label, so you can say gr.view.text, for example
    }
    

提交回复
热议问题