Last active
January 15, 2022 22:19
-
-
Save pitt500/aa8ed56e702065d1cbe809bcf8c7428c to your computer and use it in GitHub Desktop.
DragGesture demo showed in Swift and Tip: https://youtu.be/muHDX4ij_EQ
This file contains hidden or 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
// | |
// GesturesDemo.swift | |
// DragGesture_SwiftUI | |
// | |
// Created by Pedro Rojas on 10/01/22. | |
// | |
// See the full video here: https://youtu.be/muHDX4ij_EQ | |
import SwiftUI | |
struct GesturesDemo: View { | |
@GestureState var locationState = CGPoint(x: 100, y: 100) | |
@State var location = CGPoint(x: 100, y: 100) | |
var body: some View { | |
VStack { | |
// Color.green | |
// .frame(height: 200) | |
Circle() | |
.fill(.red) | |
.frame(width: 100, height: 100) | |
.position(locationState) | |
//.position(location) | |
.gesture( | |
DragGesture( | |
minimumDistance: 200, | |
coordinateSpace: .local | |
) | |
.onChanged { value in | |
self.location = value.location | |
} | |
.onEnded { value in | |
withAnimation { | |
self.location = CGPoint(x: 100, y: 100) | |
} | |
} | |
.updating( | |
self.$locationState | |
) { currentState, pastLocation, transaction in | |
pastLocation = currentState.location | |
transaction.animation = .easeInOut | |
} | |
) | |
} | |
} | |
} | |
struct GesturesDemo_Previews: PreviewProvider { | |
static var previews: some View { | |
GesturesDemo() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment