【问题标题】:More than 5 UILongPressGestureRecognizers in an App一个应用程序中超过 5 个 UILongPressGestureRecognizers
【发布时间】:2015-03-14 12:36:02
【问题描述】:

在我的 swift ios 应用程序中。我使用下面的代码在我的屏幕上创建一个圆形 UIView。然后我向它添加一个 UILongPressGestureRecognizer ,将视图本身以及gestureRecognizer 保留在它们各自的数组中。每次用户触摸并按住此视图时,代码都会再次运行并创建另一个视图并向其中添加另一个 UILongPressGestureRecognizer。

    let x = CGFloat(arc4random_uniform(UInt32(sW-w)))
    let y = CGFloat(arc4random_uniform(UInt32(sH-h-10)))+15

    circle.append(UIButton())
    touch.append(UILongPressGestureRecognizer())
    let l = circle.count-1

    circle[l].frame = CGRect(x:x, y:y, width:70, height:70)

    circle[l].backgroundColor = UIColor.redColor()
    circle[l].layer.cornerRadius = w/2
    circle[l].clipsToBounds = true

    touch[l].addTarget(self, action: "touched:")
    touch[l].minimumPressDuration = 0
    touch[l].numberOfTouchesRequired = 1
    touch[l].numberOfTapsRequired = 0
    touch[l].allowableMovement = 0
    touch[l].delegate = self

    circle[l].addGestureRecognizer(touch[l])
    self.view.addSubview(circle[l])

现在,当我运行应用程序时,我可以点击并按住圆形视图,它的状态会更改为 .Began,它还会触发最多 5 个视图的 .Changed 和 .Ended 状态。但是当添加第 6 个视图时,手势识别器无法在其上工作。

Apple 文档中没有关于同时工作的手势识别器的最大数量。还有什么可能导致这种行为?

【问题讨论】:

  • 只是想更新对此问题感兴趣的每个人,经过大量研究,如here 所述,我发现 iPhone 仅限于 5 次同时触摸。

标签: ios xcode swift uigesturerecognizer


【解决方案1】:

让多个长按识别器同时工作是一种糟糕的方法。如果您没有通过他们的代表指定他们应该同时识别触摸,那么所有识别器都会在链中的前一个失败后按链做出反应。

但您也可以为所有识别器实现委托回调:

- (BOOL)gestureRecognizer:shouldRecieveTouch:

并检查触摸是否适合相应的识别器。如果是 - 您可以在此委托回调中禁用所有其他识别器,并在合适的识别器处理触摸后,再次重新启用它们。

【讨论】:

    猜你喜欢
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多