objective-c - 我不能使用 touchesBegan/Moved/Ended 和 UITapGestureRecognizer

标签 objective-c swift cocoa-touch uigesturerecognizer

这两个不能同时使用吗?最初,我在我的 ViewController 中覆盖了 (Swift) touchesBegan/Moved/Ended。

现在,我想在特定情况下将 TapGestureRecognizer 添加到特定 View ,但永远不会触发选择器/操作。

class ViewController: UIViewController, UIGestureRecognizerDelegate {
...
func addTapGesturesOnNumberPadDisplay() {
    if tapGestureRecognizerNumberPadView == nil {
        tapGestureRecognizerNumberPadView = UITapGestureRecognizer(target: self, action: "handleTap:")
        tapGestureRecognizerNumberPadView!.delegate = self
        self.numberViewDone?.addGestureRecognizer(tapGestureRecognizerNumberPadView!)
    }
}
...
func handleTap(sender: UITapGestureRecognizer) {
    //never hit

这不可能吗?我是否应该只在 touchesBegan 中实现自己的点击功能,因为无论如何我都会覆盖它,还是有办法在这里也使用 tapGestureRecognizer?

最佳答案

由于您在 View Controller 中覆盖了 touchesBegan/Moved/Ended,因此它不应该对其他 subview 中的点击手势产生任何影响。理想情况下它应该工作。请检查下面的代码,按预期工作。

class ViewController: UIViewController, UIGestureRecognizerDelegate {

@IBOutlet weak var categoryScrollView: UIScrollView!
var customView: UIView!
var tapGestureRecognizerNumberPadView : UITapGestureRecognizer?

override func viewDidLoad() {
    super.viewDidLoad()
    customView = UIView()
    customView.frame.origin = CGPointMake(50,50)
    customView.frame.size = CGSizeMake(100, 100)
    customView.backgroundColor = UIColor.blueColor()
    self.view.addSubview(customView)
    addTapGesturesOnNumberPadDisplay()
}

func addTapGesturesOnNumberPadDisplay() {
    if tapGestureRecognizerNumberPadView == nil {
        tapGestureRecognizerNumberPadView = UITapGestureRecognizer(target: self, action: "handleTap:")
        tapGestureRecognizerNumberPadView!.delegate = self
        self.customView?.addGestureRecognizer(tapGestureRecognizerNumberPadView!)
    }
}

func handleTap(sender: UITapGestureRecognizer) {
    print("handleTap")
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

}

请检查“numberViewDone” View 是否有任何其他手势。

关于objective-c - 我不能使用 touchesBegan/Moved/Ended 和 UITapGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35152443/

相关文章:

ios - 在 Objective C 中使用发布的最佳实践

ios - 通过在 iOS 中安装 Gmail 应用凭据登录 Google 帐户

iphone - 释放您失去踪迹的对象的最佳方法

ios - HKStatisticsCollectionQuery获取心率健康套件

objective-c - 使用for循环创建数组

ios - 对成员 'collectionView' Swift 4 的模糊引用

ios - 适用于 iOS 的 KIF 框架 : Can it simulate touch-and-hold gesture?

objective-c - 首次启动应用程序时 iCloud 不工作

ios - 在 iPhone 应用程序中将图像加载到内存中

swift 错误 : 'Sequence' requires the types 'T' and 'ArraySlice<T>' be equivalent