用我的手指更准确地拖动 SKSprite 节点或 UIImage,保持偏移?
drag a SKSprite Node or a UIImage by my finger more accurately, maintain offset?
我想用手指更准确地拖动 SKSprite 节点或 UIImage。例如,如果在节点的底角注册了一个触摸并移动了,我希望图像保持手指位置与节点中心之间的距离。目前中心位置跳转到手指位置
这是我的代码,似乎没有维护偏移代码 (touchl - previousLocation)。
有什么建议吗?
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let touchl = touch.location(in: self)
let previousLocation = touch.previousLocation(in: self)
triangle.position.x = touchl.x + (touchl.x - previousLocation.x)
triangle.position.y = touchl.y + (touchl.y - previousLocation.y)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let touchl = touch.location(in: self)
let previousLocation = touch.previousLocation(in: self)
let dx = touchl.x - previousLocation.x
let dy = touchl.y - previousLocation.y
triangle.position.x = triangle.position.x + dx
triangle.position.y = triangle.position.y + dy
}
}
我想用手指更准确地拖动 SKSprite 节点或 UIImage。例如,如果在节点的底角注册了一个触摸并移动了,我希望图像保持手指位置与节点中心之间的距离。目前中心位置跳转到手指位置
这是我的代码,似乎没有维护偏移代码 (touchl - previousLocation)。 有什么建议吗?
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let touchl = touch.location(in: self)
let previousLocation = touch.previousLocation(in: self)
triangle.position.x = touchl.x + (touchl.x - previousLocation.x)
triangle.position.y = touchl.y + (touchl.y - previousLocation.y)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let touchl = touch.location(in: self)
let previousLocation = touch.previousLocation(in: self)
let dx = touchl.x - previousLocation.x
let dy = touchl.y - previousLocation.y
triangle.position.x = triangle.position.x + dx
triangle.position.y = triangle.position.y + dy
}
}