在iOS7中,苹果加强了对游戏开发的支持,其中一个功能就是增加了Game Controller Framework (GameController.framework)可以让你在程序中发现游戏控制器硬件并进行配置。通过Game Controller Framework可以通过无线或者蓝牙连接iOS设备。当控制器可用的时候,该Framework会通知应用程序。
Game Controller Framework 支持三种类型的控制器
• 标准的控制器:该控制器具有一个方向键,一个暂停按钮,四个按钮和两个肩部按钮。
• 扩展的控制器:该控制器跟标准控制器类似,但它可以有两个杆和四个肩部按钮。
• 扩展无线控制器:跟上面的控制器类似,但它以无线方式工作,不需要将IOS设备放置到控制器里面。
[img]/upload/attachment/0097/4664/f9537287-cacf-36f2-a48a-687a2182cd2e.png[/img]
软件环境:ios7 + xcode5
操作系统:OSX 10.8.4
实现过程:
1:将游戏控制器的Connet和Disconnet状态进行事件注册
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gameControllerDidConnect:) name:GCControllerDidConnectNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gameControllerDidDisconnect:) name:GCControllerDidDisconnectNotification object:nil]; [GCController startWirelessControllerDiscoveryWithCompletionHandler:^{ // we don't use any code here since when new controllers are found we will get notifications }]; }- (void)configureGameControllers {
实现相应注册事件的代码
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Game controller connected. Do you want to use it?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; [alert show]; } - (void)gameControllerDidDisconnect:(NSNotification *)notification { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Game controller has disconnected." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [alert show]; }- (void)gameControllerDidConnect:(NSNotification *)notification {
2:遍历所有的控制器,并对其按键进行注册
for (GCController *controller in [GCController controllers]) { [self setupController:controller ]; } } - (void)setupController:(GCController *)controller { /** 对X,Y,A,B键位进行注册 **/ GCControllerButtonValueChangedHandler XYABButtonHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) { }; if (controller.gamepad) { controller.gamepad.buttonA.valueChangedHandler = XYABButtonHandler; controller.gamepad.buttonB.valueChangedHandler = XYABButtonHandler; controller.gamepad.buttonX.valueChangedHandler = XYABButtonHandler; controller.gamepad.buttonY.valueChangedHandler = XYABButtonHandler; } if (controller.extendedGamepad) { controller.extendedGamepad.buttonA.valueChangedHandler = XYABButtonHandler; controller.extendedGamepad.buttonB.valueChangedHandler = XYABButtonHandler; controller.extendedGamepad.buttonX.valueChangedHandler = XYABButtonHandler; controller.extendedGamepad.buttonY.valueChangedHandler = XYABButtonHandler; } /** 对肩部的键位进行注册 **/ GCControllerButtonValueChangedHandler LRShoulderHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) { }; if (controller.gamepad) { controller.gamepad.leftShoulder.valueChangedHandler = LRShoulderHandler; controller.gamepad.rightShoulder.valueChangedHandler = LRShoulderHandler; } if (controller.extendedGamepad) { controller.extendedGamepad.leftShoulder.valueChangedHandler = LRShoulderHandler; controller.extendedGamepad.rightShoulder.valueChangedHandler = LRShoulderHandler; } /**对方向键进行注册 **/ GCControllerDirectionPadValueChangedHandler LRThumbHandler = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) { }; if (controller.extendedGamepad) { controller.extendedGamepad.leftThumbstick.valueChangedHandler = LRThumbHandler; controller.extendedGamepad.rightThumbstick.valueChangedHandler = LRThumbHandler; } if (controller.gamepad.dpad) { controller.gamepad.dpad.valueChangedHandler = dpadMoveHandler; } }- (void)configureConnectedGameControllers {
参考:《iOS 7 Game Development》