2015-09-28 16 views
6

के साथ गति घटनाएं कैसे प्राप्त करें क्या किसी ने यह पता लगाया है कि नए सेब टीवी रिमोट के साथ काम करने वाली गति घटनाएं कैसे प्राप्त करें? धन्यवाद।ऐप्पल टीवी रिमोट

मैं के साथ और सुपर बुला बिना

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

बुला की कोशिश की है मुझे कुछ भी नहीं देता है।

+0

आप एक भौतिक देव किट प्राप्त हुई थी या आप सिम्युलेटर का उपयोग कर रहे हैं? –

+2

भौतिक किट – CodyMace

+0

इस बारे में ऐप डेवलपर फ़ोरम से यहां एक लेख दिया गया है: https://forums.developer.apple.com/thread/18861 – Stefan

उत्तर

4

एक महान तेज उदाहरण यहां पाया जा सकता:: गति और अन्य डेटा एक मूल्य बदल ईवेंट हैंडलर जोड़कर ट्रैक किया जा सकता https://forums.developer.apple.com/message/65560#65560 यह मूल रूप से क्या डैनियल तूफान ऊपर कहा है, लेकिन निम्नलिखित इस समझ में आ गया मेरे लिए काम करना मैंने जो किया है वह यहाँ है।

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 
} 

मैं कहाँ चाहते हैं कि उसे लागू किया, मेरे मामले में एक 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)") 
    } 
} 
2

How to access motion & orientation information of remote वाया:

सभी की

पहले, एक NSNotificationCenter उपयोग करने के लिए नियंत्रकों को खोजने के लिए की जरूरत है। ऐप लॉन्च होने पर शायद ऐसा करने के लिए सबसे अच्छा है। कुछ इस तरह:

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

हम तो एक संपत्ति में डिवाइस जानकारी स्टोर करने के लिए जोड़ने के बाद निम्नलिखित कोड का उपयोग कर सकते हैं:

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

दूरस्थ प्रोफ़ाइल सूक्ष्म गेमपैड प्रोफ़ाइल का एक उपवर्ग है।

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); 
     } 
    }; 

+0

धन्यवाद, स्विफ्ट में आखिरी चरण को लिखने के बारे में कोई विचार है? साथ ही, क्या आप ऐप डिलीगेट में इसे स्थापित करने का सुझाव देंगे, और फिर मेरे ऐप में बस वहां से डेटा प्राप्त कर रहे हैं? – CodyMace

+0

मैं टीवीओ 9.1 पर यह कोशिश कर रहा हूं और केवल 'self.myController.motion.valueChangedHandler' के लिए गति अपडेट प्राप्त कर रहा हूं – JakubKnejzlik

1

सोचा मैं स्विफ्ट 4.0 वाक्य रचना के साथ CodyMace के महान जवाब अपडेट करेंगे

AppDelegate में (आपको यहां गेम कंट्रोलर भी आयात करने की आवश्यकता होगी):

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 ReactToMotionEvents { 
func motionUpdate(motion: GCMotion) -> Void 

}

और तुम कार्यान्वित करना चाहते हैं जहां

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)") 
    } 
} 
संबंधित मुद्दे