Skip to content

Instantly share code, notes, and snippets.

@pitt500
Last active January 15, 2022 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitt500/aa8ed56e702065d1cbe809bcf8c7428c to your computer and use it in GitHub Desktop.
Save pitt500/aa8ed56e702065d1cbe809bcf8c7428c to your computer and use it in GitHub Desktop.
DragGesture demo showed in Swift and Tip: https://youtu.be/muHDX4ij_EQ
//
// 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