UIPanGesture or TouchesMoving Sprites.最好的方法是什么?



我有一个相当复杂的基于SpriteKit的应用程序,所有这些都是通过编程方式制作的。 我正在移动许多SpriteNodes数组,并且有两种可能的方法来实现这一点。使用触摸移动功能,或者我可以使用UIPanGestureRecognizer以几乎相同的方式做同样的事情。

但是,哪种方式是最好的方法呢?我还有很多其他的UI手势,如旋转,捏合和双击,我担心代码冲突。

将它们结合起来是不好的做法吗?(我的意思不是同时使用这两种方法 - 我的意思是让手势读取一些触摸,而其他触摸功能( 我是否对"触摸平移"具有更高的控制力或精度? 我觉得,现在我已经在可能的情况下只使用手势工作得更好了 但最佳实践是什么?

我的一些代码。我最多有 40-50 个节点存储在正在移动的阵列中。

在触摸中移动:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
if numberOfBalls >= 0 {
for i in 0...(numberOfBalls) {
if ballBloqArray[i].contains(location) {
ballBloqArray[i].position = location
}
}
}
if numberOfBoxes >= 0 {
for i in 0...(numberOfBoxes) {
if boxBloqArray[i].contains(location) {
boxBloqArray[i].physicsBody?.pinned = false
boxBloqArray[i].position = location
boxBeingMoved = i
}
}
}
}

}

并使用PanGesture:

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(GameScene.tappedOnce(sender:)))
view.addGestureRecognizer(panGesture)
panGesture.minimumNumberOfTouches = 1
panGesture.cancelsTouchesInView = true
@objc func tappedOnce(sender:UIPanGestureRecognizer) {
if sender.state == .began{

}
if sender.state == .changed {
var location = sender.location(in: self.view)
location = self.convertPoint(fromView: location)
print("Single tap")
print(location)
if numberOfBalls >= 0 {
for i in 0...(numberOfBalls) {
if ballBloqArray[i].contains(location) {
ballBloqArray[i].position = location
}
}
}
}
}

没有最好的,他们都有优点和缺点。

重要的是保持一致。 不要将两者结合起来。 选择一种方法或另一种方法。

就个人而言,我会选择手势。 它允许您分离代码,并为您提供移动触摸时正在执行的操作的非常清晰的定义。

相关内容

最新更新