How do I drag and drop a sprite in Swift 3.0?

后端 未结 3 896
独厮守ぢ
独厮守ぢ 2020-12-16 22:32

All i\'m trying to do is be able to drag and drop a sprite across the screen. I\'ve tried the following code:

override func touchesBegan(touches: Set

        
3条回答
  •  [愿得一人]
    2020-12-16 23:16

    I have an implementation where I've subclassed a UIImageView and called it a "DraggableImage"

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
            originalPosition = self.center
        }
    
    override func touchesMoved(_ touches: Set, with event: UIEvent?) {
            if let touch = touches.first {
                let position = touch.location(in: self.superview)
                self.center = CGPoint(x: position.x, y: position.y)
            }
        }
    
    override func touchesEnded(_ touches: Set, with event: UIEvent?) {
    
            self.center = originalPosition
        }
    

提交回复
热议问题