swift – 如何使用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!")
}

有和没有电话超级给我什么都没有.

这里有一个很好的例子:
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)")  
    }  
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...