Created
February 11, 2020 04:01
-
-
Save dfrobison/b3361649f5741089c4cab86e0335da24 to your computer and use it in GitHub Desktop.
[Dragging Circle]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
// 1. | |
@State private var currentPosition: CGSize = .zero | |
@State private var newPosition: CGSize = .zero | |
var body: some View { | |
// 2. | |
Circle() | |
.frame(width: 100, height: 100) | |
.foregroundColor(Color.red) | |
.offset(x: self.currentPosition.width, y: self.currentPosition.height) | |
// 3. | |
.gesture(DragGesture() | |
.onChanged { value in | |
self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height) | |
} // 4. | |
.onEnded { value in | |
self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height) | |
print(self.newPosition.width) | |
self.newPosition = self.currentPosition | |
} | |
) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment