Search code examples
iosswiftuitapgesturerecognizeruicontrol

Adding UITapGestureRecognizer to the UIControl


I've got a slider class (class Slider: UIControl) which has been created programmatically and I want to add a double tap gesture to resize it to the default settings. Unfortunatelly I can't implement the UITapGestureRecognizer like I did before in SpriteKit.

part of the code:

class Slider: UIControl{
   ...
   let doubleTap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
   ...

   init(){
      ...
      doubleTap.numberOfTapsRequired = 1
      addGestureRecognizer(doubleTap)
   }

   func doubleTapped(){
      print("double tapped")
   }
}

For now I would like to implement just the gesture recognizer and then add what I need to do. Also I've implemented touchesMoved and touchesBegan.


Solution

  • Ok, the answer is very simple, no delegate needed.

    class Slider: UIControl{
       ...
    
       init(){
          ...
          let doubleTap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
          doubleTap.numberOfTapsRequired = 1
          addGestureRecognizer(doubleTap)
       }
    
       func doubleTapped(){
          print("double tapped")
       }
    }