我正在尝试使用UIPanGesture平移SKSpriteNode,问题是运动太快了(距离远大于手指的运动,下面是代码。我怀疑我添加了当前位置后出现了问题和翻译。 非常感谢您的帮助。
@objc func panGestureHandler(_ recognizer: UIPanGestureRecognizer) {
if recognizer.state == .began {
print("Touchdown")
var touchLocation = recognizer.location(in: recognizer.view)
touchLocation = self.convertPoint(fromView: touchLocation)
if (self.atPoint(touchLocation) is SKSpriteNode){
selectedNode = self.atPoint(touchLocation) as! SKSpriteNode
} else {
recognizer.state = .failed
}
} else if recognizer.state == .changed {
var translation = recognizer.translation(in: recognizer.view!)
translation = CGPoint(x: translation.x, y: -translation.y)
let position = selectedNode.position
selectedNode.position = CGPoint(x: position.x + translation.x, y: position.y + translation.y)
recognizer.setTranslation(CGPoint.zero, in: recognizer.view)
} else if recognizer.state == .ended {
recognizer.setTranslation(CGPoint.zero, in: recognizer.view)
}
}
答案 0 :(得分:0)
这是最终代码:
@objc func panGestureHandler(_ recognizer: UIPanGestureRecognizer) {
var touchLocation = recognizer.location(in: recognizer.view)
touchLocation = self.convertPoint(fromView: touchLocation)
if recognizer.state == .began {
print("Touchdown")
if (self.atPoint(touchLocation) is SKSpriteNode){
selectedNode = self.atPoint(touchLocation) as! SKSpriteNode
} else {
recognizer.state = .failed
}
} else if recognizer.state == .changed {
var translation = recognizer.translation(in: recognizer.view!)
//translation = self.convertPoint(fromView: translation)
translation = CGPoint(x: translation.x, y: -translation.y)
selectedNode.position = CGPoint(x: pos.x + translation.x, y: pos.y + translation.y)
recognizer.setTranslation(CGPoint.zero, in: recognizer.view)
pos = touchLocation
} else if recognizer.state == .ended {
recognizer.setTranslation(CGPoint.zero, in: recognizer.view)
}
}
Knight0fDragon,即使在更改状态下,您也是需要连接的翻译。 0x141E感谢您解释翻译。