swift – 如何使用Apple TV遥控器获取动作事件
发布时间:2020-12-14 05:38:42 所属栏目:百科 来源:网络整理
导读:有没有人想出如何使用新的Apple TV遥控器进行动作事件?谢谢. 我试过打电话 override func motionBegan(motion: UIEventSubtype,withEvent event: UIEvent?) { super.motionBegan(motion,withEvent: event) print("motion!")}override func motionEnded(moti
有没有人想出如何使用新的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!") } 有和没有电话超级给我什么都没有.
这里有一个很好的例子:
https://forums.developer.apple.com/message/65560#65560 这基本上就是Daniel Storm上面所说的,但是接着这个让它为我工作.这就是我做的. 在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,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 } 我希望它实现的地方,在我看来是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)") } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |