将 UITapGestureRecognizer 添加到 UIControl

Adding UITapGestureRecognizer to the UIControl

我有一个以编程方式创建的滑块 class (class Slider: UIControl),我想添加双击手势以将其大小调整为默认设置。不幸的是,我无法像以前在 SpriteKit 中那样实现 UITapGestureRecognizer。

部分代码:

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

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

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

现在我只想实现手势识别器,然后添加我需要做的事情。我还实现了 touchesMoved 和 touchesBegan。

好的,答案很简单,不需要委托。

class Slider: UIControl{
   ...

   init(){
      ...
      let doubleTap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
      doubleTap.numberOfTapsRequired = 1
      addGestureRecognizer(doubleTap)
   }

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