2015-09-28 15 views
6

Có ai tìm ra cách để nhận các sự kiện chuyển động hoạt động với điều khiển từ xa TV mới không? Cảm ơn.Cách nhận các sự kiện chuyển động với điều khiển từ xa của Apple TV

Tôi đã thử gọi

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

Có và không có gọi siêu mang lại cho tôi gì cả.

+0

Bạn đã nhận được một bộ dev về thể chất hoặc bạn đang sử dụng trình giả lập? –

+2

Bộ vật lý – CodyMace

+0

Đây là một bài viết từ diễn đàn Nhà phát triển ứng dụng về điều này: https://forums.developer.apple.com/thread/18861 – Stefan

Trả lời

4

Một ví dụ nhanh chóng tuyệt vời có thể được tìm thấy ở đây: https://forums.developer.apple.com/message/65560#65560 Đó là cơ bản những gì Daniel bão nói trên, nhưng sau này đã nhận nó làm việc cho tôi. Đây là những gì tôi đã làm.

Trong 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 
} 

Nơi tôi muốn nó được thực hiện, trong trường hợp của tôi một 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

Via How to access motion & orientation information of remote:


Trước hết, chúng ta cần sử dụng NSNotificationCenter để tìm ra bộ điều khiển. Có lẽ tốt nhất để làm điều này khi ứng dụng khởi chạy. Một cái gì đó như thế này:

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

Sau đó chúng tôi có thể sử dụng đoạn mã sau sau khi kết nối để lưu trữ các thông tin thiết bị trong một tài sản:

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

Các hồ sơ từ xa là một lớp con của hồ sơ gamepad vi. Motion và các dữ liệu khác có thể được theo dõi bằng cách thêm một giá trị thay đổi xử lý sự kiện:

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

Cảm ơn bạn, bất kỳ ý tưởng nào về cách viết bước cuối cùng đó trong Swift? Ngoài ra, bạn sẽ đề xuất thiết lập nó trong appDelegate, và sau đó trong suốt ứng dụng của tôi chỉ nhận được dữ liệu từ đó? – CodyMace

+0

Tôi đang thử trên TVOS 9.1 và chỉ nhận được cập nhật chuyển động cho 'self.myController.motion.valueChangedHandler' – JakubKnejzlik

1

Nghĩ rằng tôi sẽ cập nhật câu trả lời tuyệt vời CodyMace với cú pháp Swift 4,0

Trong appdelegate (Bạn cũng sẽ cần nhập GameController tại đây):

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

Giao thức giữ nguyên

protocol ReactToMotionEvents { 
func motionUpdate(motion: GCMotion) -> Void 

}

và nơi bạn muốn thực hiện

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)") 
    } 
} 
Các vấn đề liên quan