将UITapGestureRecognizer添加到UIControl

时间:2017-04-25 22:37:25

标签: ios swift uitapgesturerecognizer uicontrol

我有一个以编程方式创建的滑块类(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。

1 个答案:

答案 0 :(得分:2)

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

class Slider: UIControl{
   ...

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

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