tap gesture recognizer - which object was tapped?

前端 未结 12 1245
萌比男神i
萌比男神i 2020-12-29 17:47

I\'m new to gesture recognizers so maybe this question sounds silly: I\'m assigning tap gesture recognizers to a bunch of UIViews. In the method is it possible to find out w

12条回答
  •  时光说笑
    2020-12-29 18:20

    in swift it quite simple

    Write this code in ViewDidLoad() function

    let tap = UITapGestureRecognizer(target: self, action: #selector(tapHandler(gesture:)))
        tap.numberOfTapsRequired = 2
        tapView.addGestureRecognizer(tap)
    

    The Handler Part this could be in viewDidLoad or outside the viewDidLoad, batter is put in extension

    @objc func tapHandler(gesture: UITapGestureRecognizer) {
        currentGestureStates.text = "Double Tap"
    } 
    

    here i'm just testing the code by printing the output if you want to make an action you can do whatever you want or more practise and read

提交回复
热议问题