【问题标题】:UIView GestureRecognizer not workingUIView GestureRecognizer 不工作
【发布时间】:2015-08-28 14:22:32
【问题描述】:

视图创建:

var myView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 40))
myView.addSubview(myLabel) // some label 
myView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(myView)

var mySwipeGesture = UISwipeGestureRecognizer(target: self, action: "userDidSwipe:")
myView.userInteractionEnabled = true
myView.addGestureRecognizer(mySwipeGesture)

被调用的方法:

func userDidSwipe(sender:UISwipeGestureRecognizer) {
    if sender.direction == UISwipeGestureRecognizerDirection.Right {
        switchToMain()
    }
}

这不起作用,没有调用方法userDidSwipe。 奇怪的是相同的手势识别器mySwipeGesture被添加到另一个UIView(包含许多子视图)并且完美运行!

为什么它不适用于 myView ?我应该检查什么?

【问题讨论】:

  • 请显示方法userDidSwipe。
  • 要添加到的视图类型是什么?

标签: ios objective-c swift uiview uigesturerecognizer


【解决方案1】:

通常这是有效的。可能会出现以下问题,

1) 检查 myView 的 userInteractionEnabled 属性。

2) 检查 myView 的框架(可能你在不同的框架上滑动)。

3) myView 上可能有另一个视图阻止了手势。

【讨论】:

    【解决方案2】:

    您正在向myView 添加另一个手势,而不是mySwipeGesture

    更换你的线路

    myView.addGestureRecognizer(swipeGesture)
    

    myView.addGestureRecognizer(mySwipeGesture)
    

    【讨论】:

    • 我正在添加正确的手势识别器。在问题中复制我的代码只是一个错误。我纠正了它。对不起。
    【解决方案3】:

    在你没有告诉滑动手势方向的代码中添加这一行

     mySwipeGesture.direction = .right
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多