【问题标题】:Strange delay with touchesBegan in iOS 10iOS 10 中 touchesBegan 的奇怪延迟
【发布时间】:2016-09-14 14:15:12
【问题描述】:

我最近更新到 iOS10 和 Swift 3。并且知道,每当我在 iOS10 设备(在我的情况下为 iPhone6s)上运行我的应用程序时,touchesBegan 函数都会随机发生延迟。有时这会伴随以下错误消息:

"手势:之前接收系统手势状态通知失败 下一次触摸”

有趣的是,我运行 iOS9 的旧 iPhone5s 不会重现此问题。

【问题讨论】:

  • 我在iOS10中遇到了一些3D touch相关的问题。出于好奇,如果您在设备上的“通用”->“辅助功能”中禁用 3D 触控,问题会消失吗?
  • @Columbo 实际上是的!我刚刚关闭了 3d touch,问题就停止了。问题是,它只发生在屏幕的左下方,所以我只是假设我的设备有问题。希望这只是一个软件问题。
  • 您最好的选择可能是向 Apple 提交错误报告。

标签: ios swift ios10


【解决方案1】:

iOS 上奇怪的 UI 延迟通常与线程有关。 iOS 和 Mac 上的 UI 相关方法都只能从主线程调用。由于 API 变得高度异步,许多方法采用块回调,因此很容易从后台线程意外执行与 UI 相关的操作。

确保任何与 UI 相关的活动,无论是更新图像、文本字段、重新加载表格视图等,始终从主线程完成。如果你在一个块中做 UI 工作,你可以将 UI 部分包装在 dispatch_async(dispatch_get_main_queue(), ...) 中。

根据我的经验,对这些方法之一的无意的后台线程调用可能会产生“毒化井”的效果,从而导致后续的怪异现象,例如您看到的延迟触摸事件。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我模拟触摸并按住来左右驾驶汽车。这是为我解决的问题。

    从以下更改:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let location = touch.location(in: self)
            let node: SKNode = atPoint(location)
    
            if node == self.player1CarRight {
                    self.player1CarRotateRightTouching = true
            }
        }
    }
    

    收件人:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch:UITouch = touches.first! as UITouch
        let touchLocation = touch.location(in: self)
    
        if player1CarRight.contains(touchLocation) {
            self.player1CarRotateRightTouching = true;
        }
    }
    

    touchesEnded 的相同变化:

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
           let touch:UITouch = touches.first! as UITouch
           let touchLocation = touch.location(in: self)
    
            if player1CarRight.contains(touchLocation) {
                self.player1CarRotateRightTouching = false
            }
        }
    

    【讨论】:

      【解决方案3】:

      我今天遇到了这种行为,但可能是出于不同的原因。我的场景涉及在实现 touchesBegan 的视图的超级视图上有一个 UITapGestureRecognizer。由于 UITapGestureRecognizer 正在识别点击,并且因为点击识别器上 cancelsTouchesInView 的默认值为 YES,所以一旦识别器识别到触摸,视图就不会收到任何触摸回调。但是,如果我触摸并按住,UITapGestureRecognizer 无法识别触摸,然后视图会收到回调。这会导致对 touchesBegan 的调用看似延迟。

      我还注意到作为调查的一部分,UIButton 在这里得到了特殊处理。如果 UITapGestureRecognizer 正在运行,它不会考虑对 UIButtons 的触摸。

      【讨论】:

        猜你喜欢
        • 2014-01-04
        • 1970-01-01
        • 1970-01-01
        • 2017-06-02
        • 2014-07-31
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多