如何使用Apple TV遥控器获取动作事件

时间:2015-09-28 22:04:27

标签: swift tvos apple-tv

有没有人想出如何使用新的Apple TV遥控器进行动作事件?感谢。

我试过调用

override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
    super.motionBegan(motion, withEvent: event)
    print("motion!")
}
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
    super.motionEnded(motion, withEvent: event)
    print("motion ended!")
}

无论有没有打电话给我都没有。

3 个答案:

答案 0 :(得分:5)

这里有一个很好的例子: https://forums.developer.apple.com/message/65560#65560 它基本上就是Daniel Storm所说的,但在此之后让它为我工作。这就是我的所作所为。

在appDelegate中:

$sql2="select * from student_information where student_id='$id'";
$result2=mysqli_query($conn,$sql2);

if($result2->num_rows > 0){
    echo '<table width="100%" bgcolor="#FFFFFF" cellpadding="0" cellspacing="0" border="1" class="db-table">';
    echo '<tr><th>STUDENT__FIRST_NAME</th><th>STUDENT__LAST_NAME</th><th>STUDENT_ID</th><th>ADDRESS</th><th>CITY</th><th>STATE</th><th>ZIP</th><th>COUNTRY</th><th>PHONE</th><th>DEPARTMENT</th><th>MAJOR</th>
<th>EMAIL</th><th>MAILING_ADDRESS</th><th>HEALTH_INSURANCE</th><th>CREDIT_HOURS</th><th>GROUP</th><th>GENDER</th><th>DOB</th><th>STATUS</th><th>NOTES</th><th>JOINED_ON</th></tr>';

    while($rowz2 = mysqli_fetch_assoc($result2)){
        echo "<tr>";
        foreach($rowz2 as $key=>$value){
            echo '<td>',$value,'</td>';
        }
        echo '</tr>';
    }
    echo "</table><br/>";
}
else {
   echo "<h2>No data based on the entered values</h2>";
}

我希望它实现的地方,在我的例子中是SKScene:

 var motionDelegate: ReactToMotionEvents? = nil   

     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {  
        let center = NSNotificationCenter.defaultCenter()  
        center.addObserver(self, selector: "setupControllers:", name: GCControllerDidConnectNotification, object: nil)  
        center.addObserver(self, selector: "setupControllers:", name: GCControllerDidDisconnectNotification, object: nil)  
        GCController.startWirelessControllerDiscoveryWithCompletionHandler { () -> Void in  

        }  
        return true  
    }  

    func setupControllers(notif: NSNotification) {  
        print("controller connection")  
        let controllers = GCController.controllers()  
        for controller in controllers {  
            controller.motion?.valueChangedHandler = { (motion: GCMotion)->() in  
                if let delegate = self.motionDelegate {  
                    delegate.motionUpdate(motion)  
                }  
            }  
        }  
    }  

protocol ReactToMotionEvents {  
    func motionUpdate(motion: GCMotion) -> Void  
}  

答案 1 :(得分:2)

通过How to access motion & orientation information of remote

首先,需要使用NSNotificationCenter来查找控制器。当应用程序启动时,可能最好这样做。像这样:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidConnect:) name:GCControllerDidConnectNotification object:nil];  

我们可以在连接后使用以下代码将设备信息存储在属性中:

- (void)controllerDidConnect:(NSNotification *)notification {  
    self.myController = notification.object;  
}  

远程配置文件是微型游戏手柄配置文件的子类。可以通过添加值更改事件处理程序来跟踪运动和其他数据:

  GCMicroGamepad *profile =  self.myController.microGamepad  
  profile.valueChangedHandler= ^ (GCMicroGamepad *gamepad, GCControllerElement *element) {  
        if (self.myController.motion) {  
            NSLog(@"motion supported");  
            NSLog(@"gravity: %f %f %f", self.myController.motion.gravity.x, self.myController.motion.gravity.y, self.myController.motion.gravity.z);  
            NSLog(@"userAcc: %f %f %f", self.myController.motion.userAcceleration.x, self.myController.motion.userAcceleration.y, self.myController.motion.userAcceleration.z);  
            NSLog(@"rotationRate: %f %f %f", self.myController.motion.rotationRate.x, self.myController.motion.rotationRate.y, self.myController.motion.rotationRate.z);  
            NSLog(@"attitude: %f %f %f %f", self.myController.motion.attitude.x, self.myController.motion.attitude.y, self.myController.motion.attitude.z, self.myController.motion.attitude.w);  
        }  
    };  

答案 2 :(得分:1)

以为我会用Swift 4.0语法更新CodyMace的优秀答案

在appDelegate中(你也需要在这里导入GameController):

*:focus {
  outline: none !important;
}

协议保持不变

var motionDelegate: ReactToMotionEvents? = nil


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let center = NotificationCenter.default
    center.addObserver(self, selector: #selector(setupControllers), name: NSNotification.Name.GCControllerDidConnect, object: nil)
    center.addObserver(self, selector: #selector(setupControllers), name: NSNotification.Name.GCControllerDidDisconnect, object: nil)
    GCController.startWirelessControllerDiscovery { () -> Void in

    }
    return true
}

@objc func setupControllers(notif: NSNotification) {
    print("controller connection")
    let controllers = GCController.controllers()
    for controller in controllers {
        controller.motion?.valueChangedHandler = { (motion: GCMotion)->() in
            if let delegate = self.motionDelegate {
                delegate.motionUpdate(motion: motion)
            }
        }
    }
}

}

您想要实施的地方

protocol ReactToMotionEvents {
func motionUpdate(motion: GCMotion) -> Void