3

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

4

1 回答 1

1

我玩过 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 */
}

}

于 2015-09-27T18:32:00.467 回答