How to get motion events using your Apple TV remote

Has anyone understood how to make motion events work with the new pool of Apple TV? Thanks.

I tried to call

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!") } 

With a super call and without a call it gives me nothing.

+6
source share
3 answers

Here you can find a great example: https://forums.developer.apple.com/message/65560#65560 This is basically what Daniel Storm said above, but after that he worked for me. Here is what I did.

In appDelegate:

  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 } 

Where I want this to be implemented, in my case, SKScene:

 import SpriteKit import GameController class GameScene: SKScene, ReactToMotionEvents { override func didMoveToView(view: SKView) { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.motionDelegate = self } func motionUpdate(motion: GCMotion) { print("x: \(motion.userAcceleration.x) y: \(motion.userAcceleration.y)") } } 
+4
source

Via How to access information about movement and orientation of a remote :


First of all, you need to use NSNotificationCenter to search for controllers. It is probably best to do this when the application starts. Something like that:

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

Then we can use the following code after connecting to store information about the device in the property:

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

The remote profile is a subclass of the micro-controller profile. You can track movement and other data by adding an event handler value:

  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
source

I think I would update CodyMace a great answer with Swift 4.0 syntax

In appDelegate (you will need to import GameController too):

 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 remains the same

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

}

And where do you want to implement

 import SpriteKit import GameController class GameScene: SKScene, ReactToMotionEvents { override func didMoveToView(view: SKView) { let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.motionDelegate = self } func motionUpdate(motion: GCMotion) { print("x: \(motion.userAcceleration.x) y: \(motion.userAcceleration.y)") } } 
+1
source

All Articles