【问题标题】:Swift, iOS: How to detect GCController button being held when only given a pressedChangedHandler?Swift,iOS:仅在给定pressedChangedHandler时如何检测GCController按钮被按住?
【发布时间】:2021-06-07 17:07:49
【问题描述】:

我的解决方案很老套,坦率地说甚至不起作用,因为旋转循环会阻止其他同时发生的按钮动作被识别。我希望能够使用长按手势识别器,但这在 GCController 上不可用。

    let dPadLeftButtonPressedChangedHandler = {
        (dPadLeftButton: GCControllerButtonInput, value: Float, pressed: Bool) -> Void in
        
        if dPadLeftButton.isPressed {
            // No longer nil
            self.moveLeft()
            self.nanoTimer = DispatchTime.now()

            while (dPadLeftButton.isPressed) {
                let nanoTimerNow = DispatchTime.now()

                // Thousandths of a second
                let timeDifference = (nanoTimerNow.uptimeNanoseconds - self.nanoTimer!.uptimeNanoseconds) / UInt64(1000000)

                // 200 / 1000 is .2 seconds
                if timeDifference > 250 {
                    self.hardLeft()
                    self.nanoTimer = nil
                    break
                }
            }
        }
    }


    controller?.extendedGamepad?.dpad.left.pressedChangedHandler = dPadLeftButtonPressedChangedHandler

【问题讨论】:

    标签: swift button controller


    【解决方案1】:

    我通过初始化 CADisplayLink 对象来解决这个问题,以通过选择器参数调用我想要重复的函数。然后在按钮释放时,我使 CADisplayLink 无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-14
      • 2012-10-10
      • 2012-07-27
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2015-12-20
      • 2010-09-15
      相关资源
      最近更新 更多