【问题标题】:drag a SKSprite Node or a UIImage by my finger more accurately, maintain offset?用手指更准确地拖动 SKSprite 节点或 UIImage,保持偏移?
【发布时间】:2020-11-04 21:58:16
【问题描述】:

我正在尝试用手指更准确地拖动 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)
                       }
            }

【问题讨论】:

    标签: swift xcode sprite-kit uikit draggable


    【解决方案1】:
    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
            }
           }
    

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多