【问题标题】:Get the scroll direction on swift快速获取滚动方向
【发布时间】:2019-12-30 10:42:46
【问题描述】:

我正在制作一个贪吃蛇游戏。要移动蛇,玩家必须在屏幕上滚动。我真的需要知道哪个是卷轴的方向。 起初我尝试了这个:我得到了玩家触摸的位置(pointOfTouch)以及前一个触摸点(previousPointOfTouch)。 然后,如果previousPointOfTouch.X 减去pointOfTouch.X 大于0,则蛇向右移动。 左、上和下也是如此(在上和下我得到 Y 坐标)

它应该有效,而且确实有效。但是只有一个问题,它太精确了。如果我向上滚动,就像我在网上冲浪时所做的那样,应用程序会发现一些向左或向右的小动作,然后左右移动蛇。 我该如何解决这个问题?

这是我使用的代码

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch: AnyObject in touches{
            let pointOfTouch = touch.location(in: self)
            let previousPointOfTouch = touch.previousLocation(in: self)
            let amountDraggedX = pointOfTouch.x - previousPointOfTouch.x
            let amountDraggedY = pointOfTouch.y - previousPointOfTouch.y
            if amountDraggedY > 0 { //UP
                resetMoves() 
                moves[0] = true
            }
            else if amountDraggedY < 0 { //DOWN
                resetMoves()
                moves[1] = true
            }
            if amountDraggedX > 0 { //RIGHT
                resetMoves()
                moves[2] = true
            }
            else if amountDraggedX < 0 { //LEFT
                resetMoves()
                moves[3] = true
            }
        }
    }
  • 我声明了一个名为moves[] 的全局数组,它包含4 个元素。它控制 Snake 的移动(如果第一个元素为真,则蛇向上移动)
var moves = [false, false, false, false]
  • resetMoves() 是我用来将数组moves[] 的所有元素设置为false 的函数。然后我将我需要的项目更改为 true

【问题讨论】:

  • 看起来你想使用 UIPanGestureRecognizer。根据平移速度,您将获得手势方向
  • @Pikacz 会很棒!但实际上,我完全不知道如何使用这个......你有任何我可以看到的链接来学习如何使用这个类吗?
  • 别担心,找出这个youtube.com/watch?v=zPF_kFn8mBA

标签: swift xcode


【解决方案1】:

找到问题的解决方案。 继续文件“GameViewController.swift”。在 viewDidLoad() 函数中

        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))

        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        leftSwipe.direction = .left

        let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        upSwipe.direction = .up

        let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        downSwipe.direction = .down

        view.addGestureRecognizer(upSwipe)
        view.addGestureRecognizer(downSwipe)
        view.addGestureRecognizer(rightSwipe)
        view.addGestureRecognizer(leftSwipe)

现在,在这个函数之外写下这个:

@objc func handleSwipe(sender: UISwipeGestureRecognizer){
        if sender.state == .ended {
            switch sender.direction {
            case .up:
                //MOVES UP --> DO SOMETHING
            case .down:
                //MOVES DOWN --> DO SOMETHING
            case .right:
                //MOVES RIGHT --> DO SOMETHING
            case .left:
                //MOVES LEFT --> DO SOMETHING
            default:
                break
            }
        }
    }

这是您应该在“GameViewController.swift”文件中获得的代码

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))

        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        leftSwipe.direction = .left

        let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        upSwipe.direction = .up

        let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        downSwipe.direction = .down

        view.addGestureRecognizer(upSwipe)
        view.addGestureRecognizer(downSwipe)
        view.addGestureRecognizer(rightSwipe)
        view.addGestureRecognizer(leftSwipe)


        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            let scene = GameScene(size: CGSize(width:1536, height: 2048))
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFill

                // Present the scene
                view.presentScene(scene)

            view.ignoresSiblingOrder = true

            view.showsFPS = true
            view.showsNodeCount = true
        }
    }

    @objc func handleSwipe(sender: UISwipeGestureRecognizer){
        if sender.state == .ended {
            switch sender.direction {
            case .up:

            case .down:

            case .right:

            case .left:

            default:
                break
            }
        }
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

现在,如果你想和“GameScene.swift”文件通信,你只需要声明一个全局变量。

为此,您只需在库导入上方声明它。例如:

var movesController = [false, false, false, false]
import UIKit
import SpriteKit
import GameplayKit

然后我可以为每个案例分配一个函数:

           case .up:
                movesController[0]=true
            case .down:
                movesController[1]=true
            case .right:
                movesController[2]=true
            case .left:
                movesController[3]=true
            default:
                break

完成此操作后,即使在“GameScene.swift”文件中,您也可以访问数组movesController。

如果movesController[0] == true ---->他正在向上移动

如果 moveController[1] == true ----> 他正在向下移动

如果movesController[2] == true ---->他向右移动

如果 moveController[3] == true ----> 他正在向左移动

每次可以声明重置函数时重置数组:

func resetMoves(){
        for i in 0...3 { //I can do 4 moves, so I have to overwrite 4 items (0,1,2,3)
            movesController[i] = false
        }
    }

然后在每种情况下调用它

            case .up:
                resetMoves()
                movesController[0]=true
            case .down:
                resetMoves()
                movesController[1]=true
            case .right:
                resetMoves()
                movesController[2]=true
            case .left:
                resetMoves()
                movesController[3]=true
            default:
                break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 2017-08-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    相关资源
    最近更新 更多