1. 程式人生 > >iOS GameController連線遊戲手柄

iOS GameController連線遊戲手柄

環境:iOS 10.3.3

Xcode:  8.3.3

引用庫:  

#import <GameController/GameController.h>

在info.plist中新增:

Privacy - Bluetooth Peripheral Usage Description

一、執行配置/連線方法

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self configureGameControllers];
}

二、配置方法

//配置 GameController
- (void)configureGameControllers {
    NSLog(@"configure GameController");
    [[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
    }];
}

三、判斷連線狀態,成功則連線 GameController

//判斷連線狀態
- (void)gameControllerDidConnect:(NSNotification *)notification {
    [self configureConnectedGameControllers];
    NSLog(@"connect GameController Device success");
}

- (void)gameControllerDidDisconnect:(NSNotification *)notification {
    NSLog(@"fail to connect GameController Device");
}
//連線 GameControllers
- (void)configureConnectedGameControllers {
    for (GCController *controller in [GCController controllers]) {
        [self setupController:controller];
    }
}

四、配置按鍵

//配置按鍵
- (void)setupController:(GCController *)controller
{
    NSLog(@"setupController");
    
    /** 對X,Y,A,B鍵位進行註冊
     **/
    GCControllerButtonValueChangedHandler Y_ButtonHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
        NSLog(@"Y_button (value : %f), (pressed : %d)", value, pressed);
    };
    
    GCControllerButtonValueChangedHandler X_ButtonHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
        NSLog(@"X_button (value : %f), (pressed : %d)", value, pressed);
    };
    
    GCControllerButtonValueChangedHandler A_ButtonHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
        NSLog(@"A_button (value : %f), (pressed : %d)", value, pressed);
    };
    
    GCControllerButtonValueChangedHandler B_ButtonHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
        NSLog(@"B_button (value : %f), (pressed : %d)", value, pressed);
    };
    
    if (controller.gamepad) {
        controller.gamepad.buttonA.valueChangedHandler = A_ButtonHandler;
        controller.gamepad.buttonB.valueChangedHandler = B_ButtonHandler;
        controller.gamepad.buttonX.valueChangedHandler = X_ButtonHandler;
        controller.gamepad.buttonY.valueChangedHandler = Y_ButtonHandler;
    }
    if (controller.extendedGamepad) {
        controller.extendedGamepad.buttonA.valueChangedHandler = A_ButtonHandler;
        controller.extendedGamepad.buttonB.valueChangedHandler = B_ButtonHandler;
        controller.extendedGamepad.buttonX.valueChangedHandler = X_ButtonHandler;
        controller.extendedGamepad.buttonY.valueChangedHandler = Y_ButtonHandler;
    }
    
    /** 對肩部的鍵位進行註冊
     **/
    GCControllerButtonValueChangedHandler L_ShoulderHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
        NSLog(@"L_shoulder (value : %f), (pressed : %d)", value, pressed);
    };
    
    GCControllerButtonValueChangedHandler R_ShoulderHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
        NSLog(@"R_shoulder (value : %f), (pressed : %d)", value, pressed);
    };
    
    if (controller.gamepad) {
        controller.gamepad.leftShoulder.valueChangedHandler = L_ShoulderHandler;
        controller.gamepad.rightShoulder.valueChangedHandler = R_ShoulderHandler;
    }
    if (controller.extendedGamepad) {
        controller.extendedGamepad.leftShoulder.valueChangedHandler = L_ShoulderHandler;
        controller.extendedGamepad.rightShoulder.valueChangedHandler = R_ShoulderHandler;
    }
    
    /** 對扳機進行註冊
     **/
    GCControllerButtonValueChangedHandler L_LeftTrigger = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
        NSLog(@"L_TriggerHandler (value : %f), (pressed : %d)", value, pressed);
    };
    
    GCControllerButtonValueChangedHandler R_LeftTrigger = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
        NSLog(@"R_TriggerHandler (value : %f), (pressed : %d)", value, pressed);
    };
    
    if (controller.extendedGamepad) {
        controller.extendedGamepad.leftTrigger.valueChangedHandler = L_LeftTrigger;
        controller.extendedGamepad.rightTrigger.valueChangedHandler = R_LeftTrigger;
    }
    
    /** 對左右滑桿、方向按鍵進行註冊
     **/
    //滑桿
    GCControllerDirectionPadValueChangedHandler L_ThumbHandler = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) {
        NSLog(@"L_thumbHandler (x : %f), (y : %f)", xValue, yValue);
    };
    
    GCControllerDirectionPadValueChangedHandler R_ThumbHandler = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) {
        NSLog(@"R_thumbHandler (x : %f), (y : %f)", xValue, yValue);
    };
    //方向按鍵
    GCControllerDirectionPadValueChangedHandler dpadChangedHandler = ^(GCControllerDirectionPad *dpad, float xValue, float yValue){
        NSLog(@"dpad (x : %f), (y : %f)", xValue, yValue);
    };
    
    if (controller.extendedGamepad) {
        controller.extendedGamepad.leftThumbstick.valueChangedHandler = L_ThumbHandler;
        controller.extendedGamepad.rightThumbstick.valueChangedHandler = R_ThumbHandler;
    }
    if (controller.gamepad.dpad) {
        controller.gamepad.dpad.valueChangedHandler = dpadChangedHandler;
    }

}