微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决用手指更精确地拖动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
        }
       }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。