ios - 如何在 Xcode 中正确设置 GCController valueChangeHandler?

标签 ios xcode swift gamecontroller

我已成功连接 Steel 系列 Nimbus 双模拟 Controller ,用于在我的 iOS 和 tvOS 应用程序中进行测试。但我不确定如何正确设置 GCController 属性的 valueChangeHandler 部分。

到目前为止,我了解到 Controller 有 microGamepadgamepadextendedGamepad 类以及它们之间的区别。我还了解到,您可以检查连接到您设备的 Controller 上是否可以使用相应的 Controller 类。

但是现在我在设置 valueChangeHandler 时遇到了麻烦,因为如果我像这样设置三个 valueChangeHandler 部分,那么只有有效的 valueChangeHandler 才是最后加载的一个按此顺序:

self.gameController = GCController.controllers()[0]

self.gameController.extendedGamepad?.valueChangedHandler = { (gamepad, element) -> Void in
    if element == self.gameController.extendedGamepad?.leftThumbstick {
        //Never gets called
    }
}

self.gameController.gamepad?.valueChangedHandler = { (gamepad, element) -> Void in
    if element == self.gameController.gamepad?.dpad {
        //Never gets called   
    }
}

self.gameController.microGamepad?.valueChangedHandler = { (gamepad, element) -> Void in
    if element == self.gameController.microGamepad?.dpad {
        //Gets called
    }
}

如果我切换它们并最后调用 self.gameController.extendedGamepad.valueChangeHandler...,那么这些方法将起作用,并且 gamepadmicroGamepad 方法不会。

有人知道如何解决这个问题吗?

最佳答案

您测试哪个配置文件可用,并根据配置文件设置 valueChangedHandler。

重要的是要认识到,extendedGamepad 包含最多的功能,而 microGamepad 包含最少的功能(我认为 microGamepad 仅用于 AppleTV Remote )。因此,检查的顺序应该不同。扩展游戏 handle 具有 microGamepad 的所有功能 + 附加控件,因此在您的代码中,该方法将始终进入 microGamepad 配置文件。

Apple 在 DemoBots 示例项目中使用以下代码:

private func registerMovementEvents() {
    /// An analog movement handler for D-pads and movement thumbsticks.
    let movementHandler: GCControllerDirectionPadValueChangedHandler = { [unowned self] _, xValue, yValue in
        // Code to handle movement here ...
    }

    #if os(tvOS)
    // `GCMicroGamepad` D-pad handler.
    if let microGamepad = gameController.microGamepad {
        // Allow the gamepad to handle transposing D-pad values when rotating the controller.
        microGamepad.allowsRotation = true
        microGamepad.dpad.valueChangedHandler = movementHandler
    }
    #endif

    // `GCGamepad` D-pad handler.
    // Will never enter here in case of AppleTV remote as the AppleTV remote is a microGamepad
    if let gamepad = gameController.gamepad {
        gamepad.dpad.valueChangedHandler = movementHandler 
    }

    // `GCExtendedGamepad` left thumbstick.
    if let extendedGamepad = gameController.extendedGamepad {
        extendedGamepad.leftThumbstick.valueChangedHandler = movementHandler
    }
}

关于ios - 如何在 Xcode 中正确设置 GCController valueChangeHandler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35446369/

相关文章:

ios - 在 CALayer 中应用透明颜色

ios - MapKit 注释消失

ios - 兑换交易,删除 tableviewcell

ios - 如何在 Swift 中查看来自 HTTP GET 请求的服务器响应?

ios - 当单元格可见时在轮播中播放视频

ios - PHImageManager.requestImageForAsset 在 iOS swift 中返回低分辨率图像

ios - strip 错误 : submitTokenToBackend error, 未解析的标识符错误

ios - 我的 iOS 应用程序中的方向问题

ios - 使用 SKSpritenodes 和纹理

ios - 如何快速将 View Controller 放入 ScrollView 中