ios - 将 UITapGestureRecognizer 添加到 UIControl

标签 ios swift uitapgesturerecognizer uicontrol

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

最佳答案

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

class Slider: UIControl{
   ...

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

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

关于ios - 将 UITapGestureRecognizer 添加到 UIControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43622113/

相关文章:

ios - NSInvocationOperation 回调太快

ios - 在没有推送通知的情况下更新 iOS 角标(Badge)

ios - 无法选择表格 View 的单元格

ios - 如何在 MPSImageDilate 中编写一个 float 类型的不安全指针

swift - CAShapeLayer 填充颜色将保持白色

ios - RxSwift 中 UITapGestureRecognizer 无法识别的选择器

ios - 邀请用户使用 IOS 应用程序

ios - 有没有办法从我的网站向 iPhone 发送推送通知?

ios - 如何识别点击整个振荡 subview 以执行交互式动画

ios - UITapGesture 在 Popup 类上没有按预期工作