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:
Related Topics
Why Are Iboutlets Optionals After Swift 5 Migration
Enable + Disable Auto-Layout Constraints
Duplicating a Particle Emitter Effect in Sprite Kit
Accessing a String Enum by Index
Optionals VS Throwing Functions
How to Insert an Image Inline Uilabel in iOS 8 Using Swift
Swift Only -- Reading from Nsinputstream
How to Record Video in Realitykit
How to Setup Viewcontroller in Playgrounds
How to Fix Error: This Class Is Not Key Value Coding-Compliant for the Key Tableview.'
Uitableviewrowaction with Icon and Text
How to Install Package in Xcode via Swift Package Manager
Swiftui: How Do Style Text View with Different Font and Colour on String Subranges