用手指更精确地拖动SKSprite节点或UIImage,保持偏移量吗?

问题描述

我试图用手指更精确地拖动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
        }
       }