不调用滑动手势动作

Swipe gesture action is not called

我正在尝试为 tv 制作基本的 2D 贪吃蛇游戏。

我的手势有问题。我在 GameScene class 内的 didMoveToView(view: SKView) 方法中添加了要查看的手势。此 class 还扩展了 SKScene class。这是我的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方法。我正在模拟器上测试这个。知道为什么这不起作用吗?

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 遥控器”来显示遥控器
  • 鼠标悬停在触控板上时按住选项。
  • 滑动鼠标向侧面移动