不调用滑动手势动作

时间:2015-12-09 00:15:56

标签: swift sprite-kit tvos uiswipegesturerecognizer

我正在尝试为tv做基本的2D蛇游戏。

手势有问题。我在GameScene类的didMoveToView(view: SKView)方法中添加手势来查看。本课程还扩展了SKScene类。这是我的didMoveToView方法和handleSwipe方法。

override func didMoveToView(view: SKView) {
    let swipeUp = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
    swipeUp.direction = .Up
    view.addGestureRecognizer(swipeUp)

    let swipeDown = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
    swipeDown.direction = .Down
    view.addGestureRecognizer(swipeDown)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
    swipeLeft.direction = .Left
    view.addGestureRecognizer(swipeLeft)

    let swipeRight = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
    swipeRight.direction = .Right
    view.addGestureRecognizer(swipeRight)

    backgroundColor = SKColor.blackColor()
}

func handleSwipe(sender: UIGestureRecognizer) {
    if let gesture = sender as? UISwipeGestureRecognizer {
        switch(gesture.direction) {
            case UISwipeGestureRecognizerDirection.Right: m_snake.moveRight()
            case UISwipeGestureRecognizerDirection.Left: m_snake.moveLeft()
            case UISwipeGestureRecognizerDirection.Up: m_snake.moveUp()
            case UISwipeGestureRecognizerDirection.Down: m_snake.moveDown()
            default:
                break
        }
    }
}

当我在遥控器上滑动时,似乎我的程序没有输入handleSwipe方法。我在模拟器上测试这个。知道为什么这不起作用吗?

1 个答案:

答案 0 :(得分:3)

func handleSwipes(sender:UISwipeGestureRecognizer) {
    if let gesture = sender as? UISwipeGestureRecognizer {
        switch(gesture.direction) {
            case UISwipeGestureRecognizerDirection.Right: m_snake.moveRight()
            case UISwipeGestureRecognizerDirection.Left: m_snake.moveLeft()
            case UISwipeGestureRecognizerDirection.Up: m_snake.moveUp()
            case UISwipeGestureRecognizerDirection.Down: m_snake.moveDown()
            default:
                break
        }
    }
}

我刚刚在我当前的项目中测试了你的代码,它工作得很好

要使滑动工作,请像这样配置模拟器;

  • 转到硬件>显示遥控器显示Apple TV Remote
  • 当鼠标悬停在触控板上时按住选项。
  • 以轻扫方式向侧面移动鼠标