轻触触摸中的手势方法

星状

因此,我已经在我的游戏中实现了手势校正器,以检测玩家的动作,但是发现它们没有给我想要的结果,因此我正在尝试通过touches方法进行轻扫手势,同时在接触方法。我已经设法使touch功能在touches方法中起作用,但是我无法实现在touches方法中滑动的功能,而且我似乎找不到有关如何执行此操作的教程。下面的代码显示了我正在尝试实现的touches方法:

class GameScene: SKScene {

 var touchOrigin = CGPoint()
 var player = SKSpriteNode()


override func didMove(to view: SKView) {

    backgroundColor = .black

    player = SKSpriteNode(texture: nil, color: .orange, size: CGSize(width: 50, height: 50))
    player.position = CGPoint(x: 0, y: 0)
    addChild(player)

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch in touches {

        var currentTouchPosition = touch.location(in: self)
        touchOrigin = touch.location(in: self)

        if (Int(touchOrigin.x) > Int(currentTouchPosition.x)) {

            player.position.x -= 50

        } else if (Int(touchOrigin.x) < Int(currentTouchPosition.x)) {

            player.position.x += 50

        }

        if touch.tapCount == 1 {

            player.position.y += 50 //replace this with function :)

        } else if touch.tapCount >= 2 {

            player.position.y += 150 // change to run shield effect :)

        }
    }

}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
}

}

如何使touches方法识别特定方向的轻扫手势?如果他们朝某个方向滑动而不是将手指从屏幕上移开,然后以一次动作向后滑动回原点,那我该怎么做,以便将其识别为轻拍呢?

0x141E

这是一个如何检测滑动手势的示例。

首先,定义实例变量以存储起始位置和时间

var start:(location:CGPoint, time:TimeInterval)?

并定义滑动手势的参数。相应地调整这些:

let minDistance:CGFloat = 25
let minSpeed:CGFloat = 1000
let maxSpeed:CGFloat = 6000

在中touchesBegan,保存每个新触摸事件的位置/时间

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        start = (touch.location(in:self), touch.timestamp)
    }
}

在中touchesEnded,通过比较手势的距离和速度来确定用户的手势是否为滑动手势。对角滑动的测试是可选的。

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    var swiped = false
    if let touch = touches.first, let startTime = self.start?.time,
            let startLocation = self.start?.location {
        let location = touch.location(in:self)
        let dx = location.x - startLocation.x
        let dy = location.y - startLocation.y
        let distance = sqrt(dx*dx+dy*dy)

        // Check if the user's finger moved a minimum distance
        if distance > minDistance {
            let deltaTime = CGFloat(touch.timestamp - startTime)
            let speed = distance / deltaTime

            // Check if the speed was consistent with a swipe
            if speed >= minSpeed && speed <= maxSpeed {

                // Determine the direction of the swipe
                let x = abs(dx/distance) > 0.4 ? Int(sign(Float(dx))) : 0
                let y = abs(dy/distance) > 0.4 ? Int(sign(Float(dy))) : 0

                swiped = true
                switch (x,y) {
                case (0,1):
                    print("swiped up")
                case (0,-1):
                    print("swiped down")
                case (-1,0):
                    print("swiped left")
                case (1,0):
                    print("swiped right")
                case (1,1):
                    print("swiped diag up-right")
                case (-1,-1):
                    print("swiped diag down-left")
                case (-1,1):
                    print("swiped diag up-left")
                case (1,-1):
                    print("swiped diag down-right")
                default:
                    swiped = false
                    break
                }
            }
        }
    }
    start = nil
    if !swiped {
        // Process non-swipes (taps, etc.)
        print("not a swipe")
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

处理触摸和手势事件

来自分类Dev

侏儒-触摸板手势

来自分类Dev

触摸板手势和触摸屏手势

来自分类Dev

在跳跃动作上触摸手势的更好方法

来自分类Dev

使UIControl仅接受滑动手势并传递触摸/点击手势

来自分类Dev

将触摸和手势从UIScrollview转发到视图

来自分类Dev

Ubuntu 18.04 LTS中的触摸板手势

来自分类Dev

在Tkinter中处理触摸屏手势

来自分类Dev

WPF是否具有触摸和保持手势?

来自分类Dev

Android中的全局触摸手势?

来自分类Dev

Ubuntu 18.04 LTS中的触摸板手势

来自分类Dev

触摸板上的多点触控手势

来自分类Dev

WinRT中的触摸手势(使用monogame)

来自分类Dev

手势识别器阻止触摸已结束

来自分类Dev

GNOME 3.14中的多点触摸手势

来自分类Dev

触摸板手势可更改工作区

来自分类Dev

禁用DrawerLayout的稀松触摸手势

来自分类Dev

如何测试触摸板的多手势功能?

来自分类Dev

有没有一种方法可以为非触摸屏用户模拟手势?

来自分类Dev

限制手势

来自分类Dev

ios标签栏触摸和触摸手势出现奇怪的错误

来自分类Dev

如何在启用多点触摸的触摸板上使用触摸手势识别堆栈?

来自分类Dev

如何在启用多点触摸的触摸板上使用触摸手势识别堆栈?

来自分类Dev

Swift教程FoodTracker:未调用点击手势方法

来自分类Dev

手势导航:在ViewCompat类中缺少setSystemGestureExclusionRects()方法

来自分类Dev

在Android中将手势转换为文本的API或方法

来自分类Dev

在双击手势方法之前调用按钮动作

来自分类Dev

用捏手势限制平移手势

来自分类Dev

UIView平移手势与系统手势冲突