Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Apple TV Remote in landscape?

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

like image 247
hotpaw2 Avatar asked Nov 24 '25 18:11

hotpaw2


1 Answers

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 */
}

}

like image 91
Stefan Avatar answered Nov 28 '25 17:11

Stefan