How to Get Motion Events with the Apple Tv Remote

How to get motion events with the Apple TV remote

A great swift example can be found here:
https://forums.developer.apple.com/message/65560#65560
It's basically what Daniel Storm said above, but following this got it working for me. Here's what I did.

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

Where I want it implemented, in my case an 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)")
}
}

How to detect bumps via Apple TV Siri Remote

Tapping (without clicking the physical button) on the edges of the touchpad will synthesize a directional movement event, as described in the documentation link you provided. You should be able to detect these with one or more UITapGestureRecognizer instances configured with the allowedPressTypes appropriate for the relevant edge(s). Such a gesture recognizer will also recognize game controller dpad button presses and directional clicks from older Apple Remote devices.

The accelerometer-based action from the video should be achievable with the GCMotion API from GameControllerKit.

What events are produced by UIControl on tvOS?

A UIControl doesn't emit any control events of its own accord. Your subclass is responsible for emitting events (generally by sending itself sendActionsForControlEvents:).

Since UIControl isn't currently documented for tvOS, it might be that Apple doesn't want you to subclass it.

Anyway, I've only played around with it a little, but apparently to implement your own UIControl subclass, you must override canBecomeFocused to return YES, and you must override pressesEnded:withEvent: to act on the user's press (presumably by sending yourself sendActionsForControlEvents:.).

You might also want to override pressesBegan:withEvent: to highlight your control.

Because UIControl conforms to the UIFocusEnvironment protocol (by way of UIView), you can override didUpdateFocusInContext:withAnimationCoordinator: to update your control's appearance depending on whether it has focus.

tvOS how to recognize remote gestures for Objective C?

The Swift code from your example:

let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
swipeDown.direction = .Down
view.addGestureRecognizer(swipeDown)

func swipedDown(sender:UISwipeGestureRecognizer){
NSLog("It worked")
}

Swift 5:

let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
swipeDown.direction = .down
view.addGestureRecognizer(swipeDown)

func swipedDown(sender:UISwipeGestureRecognizer){
NSLog("It worked")
}

Translating to Objective C:

- (void)viewDidLoad {
[super viewDidLoad];

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDown:)];
recognizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:recognizer];
}

- (void)swipeDown:(UISwipeGestureRecognizer *)sender
{
NSLog(@"It works");
}

Which seems to work:

Sample Image



Related Topics



Leave a reply



Submit