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

前端之家收集整理的这篇文章主要介绍了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)")  
    }  
}
原文链接:https://www.f2er.com/swift/318628.html

猜你在找的Swift相关文章