Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS 14 Objective-C中连接多个键盘_Objective C_Ios14 - Fatal编程技术网

在iOS 14 Objective-C中连接多个键盘

在iOS 14 Objective-C中连接多个键盘,objective-c,ios14,Objective C,Ios14,iOS 14推出GCKeyboard。我能够在我的应用程序中成功连接一个蓝牙数字广告,但我需要能够串联连接其中两个。有人能告诉我示例代码吗 在GCController类中有一个“controllers”方法,它返回所有连接的控制器的数组,但是GCKeyboard没有类似的方法 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; [[UIApplication sharedApplication]setIdleTimerD

iOS 14
推出
GCKeyboard
。我能够在我的应用程序中成功连接一个
蓝牙数字广告
,但我需要能够串联连接其中两个。有人能告诉我示例代码吗

GCController
类中有一个“
controllers
”方法,它返回所有连接的控制器的数组,但是
GCKeyboard
没有类似的方法

这是我的密码:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UIApplication sharedApplication]setIdleTimerDisabled:YES];

    // notifications for keyboard (dis)connect
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWasConnected:) name:GCKeyboardDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWasDisconnected:) name:GCKeyboardDidDisconnectNotification object:nil];
}

GCKeyboard *leftKeyboard;
GCKeyboard *rightKeyboard;

- (void)keyboardWasConnected:(NSNotification *)notification {
    
    if (!self.leftKeyboard) {
        leftKeyboard = (GCKeyboard *)notification.object;
        NSLog(@"Left Keyboard connected: %@\n", leftKeyboard.description);
        NSString *keyboardStatus = [NSString stringWithFormat:@"Left Keyboard CONNECTED:\n%@", leftKeyboard.description];
        self.statusLabel.text = keyboardStatus;
        
        self.leftKeyboard = leftKeyboard;
        [self reactToKeyboardInput];
    }
    else if (!self.rightKeyboard) {
        NSLog(@"Right Keyboard connected: %@\n", rightKeyboard.description);
        rightKeyboard = (GCKeyboard *)notification.object;
        NSString *keyboardStatus = [NSString stringWithFormat:@"Right Keyboard CONNECTED:\n%@", rightKeyboard.description];
        self.statusLabel.text = keyboardStatus;
        
        self.rightKeyboard = rightKeyboard;
        [self reactToKeyboardInput];
    }
}

- (void)keyboardWasDisconnected:(NSNotification *)notification {
    
    if (self.leftKeyboard) {
        GCKeyboard *keyboard = (GCKeyboard *)notification.object;
        NSString *status = [NSString stringWithFormat:@"Left Keyboard DISCONNECTED:\n%@", keyboard.description];
        self.statusLabel.text = status;
    
        self.leftKeyboard = nil;
    }
    else if (self.rightKeyboard) {
        GCKeyboard *keyboard = (GCKeyboard *)notification.object;
        NSString *status = [NSString stringWithFormat:@"Right Keyboard DISCONNECTED:\n%@", keyboard.description];
        self.statusLabel.text = status;
    
        self.rightKeyboard = nil;
    }
}
当连接键盘或数字键盘时,我会收到以下信息:

2020-09-18 13:09:15.845943-0700 Controller[1653:351628] Left Keyboard connected: GCController 0x280bb8dd0 ('Keyboard' - 0x27c3dc28cec4d818)
 
“将键盘和鼠标游戏带到iPad上”WWDC视频记录声明:

对于键盘,您无法区分多个键盘,而来自多个键盘的所有键盘事件都会为您合并。因此,虽然您可能会使用通知来通知键盘何时断开连接,或者暂停游戏以提示不同的输入,但一般来说,您会发现仅使用GCKeyboard-Device.coalized来检查键状态,而非nil是正确的路径

因此,虽然您可能连接了多个键盘,但听起来它们都合并到一个
GCKeyboard
实例中,您无法区分它们。这就是为什么没有
+[gc键盘]
方法的原因。取而代之的是WWDC视频转录本中的
+[GCKeyboard coalescedKeyboard]

“将键盘和鼠标游戏带到iPad上”

对于键盘,您无法区分多个键盘,而来自多个键盘的所有键盘事件都会为您合并。因此,虽然您可能会使用通知来通知键盘何时断开连接,或者暂停游戏以提示不同的输入,但一般来说,您会发现仅使用GCKeyboard-Device.coalized来检查键状态,而非nil是正确的路径


因此,虽然您可能连接了多个键盘,但听起来它们都合并到一个
GCKeyboard
实例中,您无法区分它们。这就是为什么没有
+[gc键盘]
方法的原因。相反,这里有
+[GCKeyboard coalescedKeyboard]

谢谢Zorg。不幸的是,老鼠也是如此。无法区分是否有多个蓝牙鼠标配对。奇怪的是,GCKeyboard以某种方式与GCController关联,如GCKeyboard.description属性“GCController 0x280bb8dd0('Keyboard'-0x27c3dc28cec4d818)”中所示,GCController有一个playerIndex属性,可以设置为GCControllerPlayerIndex 1。我想知道GCKeyboard是否可以以某种方式强制转换到GCController,以便我可以使用此属性。有什么建议吗?根据文档,Mice没有合并。您应该能够通过通知处理程序或
+[GCMouse mices]
区分多个鼠标。GCKeyboard和GCController都是同一框架的一部分,并符合
GCDevice
协议。它们的输入属性是
GCPhysicalInputProfile
的子类。GCKeyboard没有playerIndex属性,因为它没有太多意义-键盘上没有这样的LED指示灯来显示玩家索引。感谢您将[GCMouse mices]指出来,@Zorg。我试过了,它可以很好地区分记录在案的多对老鼠。至于[GCKeyboard coalescedKeyboard],我想知道是否有办法访问GCContoller.framework源代码,以便修改GCKeyboard并添加“keyboards”属性并在本地重新编译。我知道,在大多数情况下,区分键盘并没有多大意义,但这对我的应用程序来说非常有意义:-)苹果是否公开了这些来源?如果是的话,我可以从哪里下载呢?不,那是一厢情愿的想法。如果你想获得AppStore的批准,你也不能使用私有API。您可以检查UIKit的API来处理物理键盘(我不熟悉),如果它允许区分它们的话。由于iOS堆栈中的其他原因,GCKeyboard很有可能合并。谢谢Zorg。不幸的是,老鼠也是如此。无法区分是否有多个蓝牙鼠标配对。奇怪的是,GCKeyboard以某种方式与GCController关联,如GCKeyboard.description属性“GCController 0x280bb8dd0('Keyboard'-0x27c3dc28cec4d818)”中所示,GCController有一个playerIndex属性,可以设置为GCControllerPlayerIndex 1。我想知道GCKeyboard是否可以以某种方式强制转换到GCController,以便我可以使用此属性。有什么建议吗?根据文档,Mice没有合并。您应该能够通过通知处理程序或
+[GCMouse mices]
区分多个鼠标。GCKeyboard和GCController都是同一框架的一部分,并符合
GCDevice
协议。它们的输入属性是
GCPhysicalInputProfile
的子类。GCKeyboard没有playerIndex属性,因为它没有太多意义-键盘上没有这样的LED指示灯来显示玩家索引。感谢您将[GCMouse mices]指出来,@Zorg。我试过了,它可以很好地区分记录在案的多对老鼠。至于[GCKeyboard coalescedKeyboard],我想知道是否有办法访问GCContoller.framework源代码,以便修改GCKeyboard并添加“keyboards”属性并在本地重新编译。我知道,在大多数情况下,区分键盘并没有多大意义,但这对我的应用程序来说非常有意义:-)苹果是否公开了这些来源?如果是的话,我可以从哪里下载呢?不,那是一厢情愿的想法。如果你想获得AppStore的批准,你也不能使用私有API。您可以检查UIKit的API来处理物理键盘(我不熟悉),如果它允许区分它们的话。疗法