如何使用苹果电视遥控器的横向



是否有一个tvOS API来检测横向方向和/或允许使用Apple TV控制器进行触摸手势输入,控制器保持在横向?(例如,无论控制器是纵向还是横向,应用程序都将获得一个关于地板/重力的向上滑动事件)。

我试了一下AppleTV和遥控器。遥控器上的手势识别是可能的。我从我的博客文章中复制了示例代码。

对于检测遥控器的方向,这个来自苹果开发者论坛的答案可能会有所帮助。

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

}

相关内容

  • 没有找到相关文章

最新更新