Is there a tvOS API to detect landscape orientation and/or allow use of the Apple TV controller for touch gesture input with the controller held in landscape? (e.g. so the app will get a swipe up event with respect to the floor/gravity whether the controller is portrait or turned sideways).
I've played a little bit around with the AppleTV and the remote. Gesture recognition on the remote is possible. I've copied the sample code from my Blog article about this.
For detecting the orientation of the remote this answer from the Apple Developer forum might be helpful.
import SpriteKit
class GameScene: SKScene {
let sprite = SKSpriteNode(imageNamed:"Spaceship")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// Add Sprite
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
self.addChild(sprite)
// Register Swipe Events
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
swipeUp.direction = .Up
view.addGestureRecognizer(swipeUp)
let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
swipeDown.direction = .Down
view.addGestureRecognizer(swipeDown)
}
// Handle Swipe Events
func swipedRight(sender:UISwipeGestureRecognizer){
sprite.position = CGPoint(x: sprite.position.x + 10, y: sprite.position.y)
}
func swipedLeft(sender:UISwipeGestureRecognizer){
sprite.position = CGPoint(x: sprite.position.x - 10, y: sprite.position.y)
}
func swipedUp(sender:UISwipeGestureRecognizer){
sprite.position = CGPoint(x: sprite.position.x, y: sprite.position.y+10)
}
func swipedDown(sender:UISwipeGestureRecognizer){
sprite.position = CGPoint(x: sprite.position.x, y: sprite.position.y-10)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With