环境:iOS 10.3.3

Xcode:  8.3.3

蓝牙手柄:莱仕达MFi认证手柄 链接 (非广告,只是随便搜的)

引用库:

#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;}}

五、苹果官方介绍 Game Controller Programming Guide

iOS GameController连接游戏手柄相关推荐

  1. iOS App 连接外设的几种方式

    原创作者: Max_Marry 文章地址: http://www.jianshu.com/p/852bf92c5c92 随着近年来车联网和物联网的兴起,智能家居和智能硬件的逐步火热,越来越多的 App ...

  2. 游戏手柄计算机,win7系统怎么设置连接手柄 win7电脑连接游戏手柄的方法

    ‍ ‍ 为了更好更方便地玩游戏,很多用户都会买游戏手柄来使用,那么win7系统怎么设置连接手柄?需要怎么弄才能在电脑上连接游戏手柄呢?且看以下的教程. 具体的解决方法:‍ 1.首先将手柄接入usb端口 ...

  3. 解决iOS手机连接fiddler后出现 “此站点的安全证书不受信任” 的问题!!!

    解决iOS手机连接fiddler后出现 "此站点的安全证书不受信任" 的问题!!! 配置Fiddler 设置iOS设备代理 之前想用Fiddler来抓取iOS手机App数据,在网上 ...

  4. 解决iOS手机连接fiddler后出现此站点的安全证书不受信任 的问题

     关闭Fiddler点击 此处   安装 Fiddlercertmaker.(!!!注意这步是解决问题的关键.!!!) 安装完成后,打开Fiddler,打开"Tools">& ...

  5. ios手机连接远程服务器,04-iOS逆向工程使用SSH通过USB远程登录越狱iPhone

    前言 iOS默认是使用22端口进行SSH通信,采用的是TCP协议,所以要求客户端(Mac)和服务器端(iPhone)在同一局域网中,如在同一WiFi中. 为了解除这种限制和加快传输速度,也可以通过US ...

  6. iOS 蓝牙连接小米手环

    转自:http://blog.csdn.net/li_yangyang_li/article/details/51223615?locationNum=2&fps=1 1 前言 当前有越来越多 ...

  7. 计算机连接游戏手柄,电脑如何使用手柄_电脑怎么连手柄打游戏-系统城

    现在随着游戏的火爆,很多种游戏都开始支持游戏手柄.游戏爱好者们为了有更好的游戏体验,有时候会把手柄安装到电脑上面.但有些朋友还不会将电脑和手柄连接起来,那么电脑如何才能使用手柄呢?接下来小编就给大家带 ...

  8. iOS 获取连接的WiFi和Mac地址

    最近做了一个WiFi的硬件APP,需要用到当前手机链接的WiFi名字和Mac地址,这种场景使用,其实,在打卡软件中,应该比较常用,比如打卡需要连接到公司的WiFi,就是保证你在办公室打的卡. 刚开始使 ...

  9. 苹果手机显示无法连接游戏服务器,《Disorder》手游无法连接服务器解决方法 IOS无法连接服务器怎么办...

    导 读 Disorder手游最近有部分IOS玩家反映iOS10无法连接到<Disorder>游戏服务器的情况,出现这个情况的原因是国行版本的iPhone及可插sim卡的iPad升级iOS1 ...

最新文章

  1. 远程办公在美国有多受欢迎?有人宁愿降薪 50% 也不愿再回办公室
  2. OFDM专题之子载波间干扰问题(二)
  3. mysql管理员操作
  4. QT 使用全局变量的方法
  5. OpenCV霍夫圈检测Hough Circle Detection的实例(附完整代码)
  6. N个富文本编辑器/基于Web的HTML编辑器
  7. 活动目录组策略统一管理桌面
  8. [react] React16废弃了哪些生命周期?为什么?
  9. Javascript 刷新框架及页面的方法汇总(转)
  10. linux shell 子进程结束,关于linux:如何终止以shell = True启动的python子进程
  11. 阿里云ECS+Nginx+nginx_rtmp_module+FFMPEG服务器搭建过程
  12. MCGS7.7嵌入版软件安装方法
  13. 高速公路匝道口事故何时了?
  14. 磨金石教育摄影干货分享|风光摄影后期教程:冷色调变暖色调
  15. 解决npm public提示403 Forbidden - PUT https://registry.npmjs.org/js-toolkit - You do not have permission
  16. stimulsoft oracle,【Stimulsoft Reports Java教程】使用Oracle数据库
  17. 解决vs2019中vsvim无法使用ctrl+[的问题
  18. leetcode每日一题—781.森林中的兔子
  19. 线性运放HCNR201-500E构成的模拟信号隔离放大电路
  20. 机器人 瓷砖墙面清洗_卫生间瓷砖怎么清洗 教你几招让卫生间墙面光洁如新

热门文章

  1. 找到PHP工作并遇上心仪女孩,求大神支招怎么约饭?
  2. 中国建设银行网上支付接口以及自动对账
  3. 你必须知道的印刷工艺 x 书籍设计
  4. 如何用天纵手机APP快速开发框架开发手机APP
  5. iOS SDK开发系列三之微信支付SDK封装Demo以及xcodebuild简单打包脚本实现
  6. 自己写论文的经验整理
  7. python线段树编写,很冗长,不过不难理解。
  8. 二十多岁该做些什么,将来才不会后悔?
  9. order by,sort by,distribute by,cluster by的区别是什么?
  10. SQL语句排序中null值的处理