Has anybody figured out how to get motion events working with the new apple TV remote? Thanks.
I've tried calling
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 and without calling super gives me nothing.
A great swift example can be found here: https://forums.developer.apple.com/message/65560#65560 It's basically what Daniel Storm said above, but following this got it working for me. Here's 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 it implemented, in my case an 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)")
}
}