我在 Apple TV 模拟器中遇到问题。我有 Xcode 9.4 并且正在使用 Nimbus 无线控制器。我有一个游戏的菜单屏幕,它不注册扩展游戏手柄上的按钮点击。当我使用它们时,模拟器会看到摇杆和方向键并进行注册,但没有一个按钮起作用。这是我的代码。这是代码。有人可以回顾一下这个并告诉我为什么他们认为这不起作用吗?谢谢,格雷格
import UIKit
import GameController
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupControllerObservers()
connectControllers()
}
func setupControllerObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(connectControllers), name: NSNotification.Name.GCControllerDidConnect, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(controllerDisconnected), name: NSNotification.Name.GCControllerDidDisconnect, object: nil)
}
@objc func connectControllers() {
for controller in GCController.controllers() {
if (controller.extendedGamepad != nil && controller.playerIndex == .indexUnset) {
controller.playerIndex = .index1
controller.extendedGamepad?.valueChangedHandler = nil
setUpExtendedController(controller: controller)
} else if (controller.gamepad != nil && controller.playerIndex == .indexUnset) {
controller.playerIndex = .index1
controller.gamepad?.valueChangedHandler = nil
setUpStandardController(controller: controller)
}
}
}
enum stick {
case dPad
case leftThumbStick
case rightThumbStick
}
enum direction {
case up
case down
case left
case right
}
enum button {
case a
case b
case x
case y
case leftShoulder
case leftTrigger
case rightShoulder
case rightTrigger
}
func move(whichStick: stick, whichDirection: direction) {
print("moved")
}
func pressed(whichButton: button) {
print("pressed")
}
func setUpExtendedController(controller:GCController) {
controller.extendedGamepad?.valueChangedHandler = {
(gamepad: GCExtendedGamepad, element: GCControllerElement) in
if (gamepad.buttonA.isPressed) {
print("tseting button a pressed")
self.pressed(whichButton: button.a)
}
if (gamepad.buttonX.isPressed) {
self.pressed(whichButton: button.x)
}
if (gamepad.buttonY.isPressed) {
self.pressed(whichButton: button.y)
}
if (gamepad.rightShoulder.isPressed) {
self.pressed(whichButton: button.rightShoulder)
}
if (gamepad.rightTrigger.isPressed) {
self.pressed(whichButton: button.rightTrigger)
}
if (gamepad.leftShoulder.isPressed) {
self.pressed(whichButton: button.leftShoulder)
}
if (gamepad.leftTrigger.isPressed) {
self.pressed(whichButton: button.leftTrigger)
}
if (gamepad.leftThumbstick == element) {
if (gamepad.leftThumbstick.up.value > 0.2) {
self.move(whichStick: stick.leftThumbStick, whichDirection: direction.up)
} else if (gamepad.leftThumbstick.left.value > 0.2) {
self.move(whichStick: stick.leftThumbStick, whichDirection: direction.left)
} else if (gamepad.leftThumbstick.down.value > 0.2) {
self.move(whichStick: stick.leftThumbStick, whichDirection: direction.down)
} else if (gamepad.leftThumbstick.right.value > 0.2) {
self.move(whichStick: stick.leftThumbStick, whichDirection: direction.right)
}
} else if (gamepad.dpad == element) {
if (gamepad.dpad.down.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.down)
} else if (gamepad.dpad.left.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.left)
} else if (gamepad.dpad.right.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.right)
} else if (gamepad.dpad.up.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.up)
}
} else if (gamepad.rightThumbstick == element) {
if (gamepad.leftThumbstick.up.value > 0.2) {
self.move(whichStick: stick.rightThumbStick, whichDirection: direction.up)
} else if (gamepad.leftThumbstick.left.value > 0.2) {
self.move(whichStick: stick.rightThumbStick, whichDirection: direction.left)
} else if (gamepad.leftThumbstick.down.value > 0.2) {
self.move(whichStick: stick.rightThumbStick, whichDirection: direction.down)
} else if (gamepad.leftThumbstick.right.value > 0.2) {
self.move(whichStick: stick.rightThumbStick, whichDirection: direction.right)
}
}
}
}
func setUpStandardController(controller:GCController) {
controller.gamepad?.valueChangedHandler = {
(gamepad: GCGamepad, element:GCControllerElement) in
if (gamepad.dpad == element) {
if (gamepad.dpad.down.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.down)
} else if (gamepad.dpad.left.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.left)
} else if (gamepad.dpad.right.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.right)
} else if (gamepad.dpad.up.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.up)
}
} else if (gamepad.buttonA == element) {
self.pressed(whichButton: button.a)
//} else if (gamepad.buttonB == element) {
// self.pressed(whichButton: button.b)
} else if (gamepad.buttonX == element) {
self.pressed(whichButton: button.x)
} else if (gamepad.buttonY == element) {
self.pressed(whichButton: button.y)
} else if (gamepad.rightShoulder == element) {
self.pressed(whichButton: button.rightShoulder)
} else if (gamepad.leftShoulder == element) {
self.pressed(whichButton: button.leftShoulder)
}
}
}
@objc func controllerDisconnected() {
}
}
我找到了我的问题。我需要使用buttonA ==元素而不是buttonA.isPressed。
除此之外,tvOS 模拟器无法识别 GCMicroGamepad。
https://developer.apple.com/documentation/gamecontroller/gccontroller/1627772-microgamepad
它只能识别使用与 Mac 配对的物理 MFi 设备的扩展游戏手柄。
要测试 GCMicroGamepad,您需要将 Test Flight 与物理 Apple TV 结合使用。
如果您为 tvOS 启用 GCMicroGamepad,实体 Apple TV 会自动将 Siri Remote 识别为微型游戏手柄。它不适用于 tvOS 模拟器。