Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fluidgroup/swiftui-gesture-velocity
In SwiftUI, a property-wrapper provides velocity in pt/s from gesture
https://github.com/fluidgroup/swiftui-gesture-velocity
swiftui
Last synced: about 2 months ago
JSON representation
In SwiftUI, a property-wrapper provides velocity in pt/s from gesture
- Host: GitHub
- URL: https://github.com/fluidgroup/swiftui-gesture-velocity
- Owner: FluidGroup
- License: mit
- Created: 2022-09-10T11:46:25.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-06T18:15:16.000Z (over 1 year ago)
- Last Synced: 2024-10-14T19:40:25.727Z (3 months ago)
- Topics: swiftui
- Language: Swift
- Homepage:
- Size: 21.5 KB
- Stars: 42
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swiftui-GestureVelocity
In SwiftUI, a property-wrapper provides velocity in pt/s from gesture
## Instructions
```swift
@GestureVelocity private var velocity: CGVector
``````swift
.gesture(
DragGesture(...)
... some declarations
.updatingVelocity($velocity)
```## Example
![CleanShot 2023-04-08 at 16 32 28](https://user-images.githubusercontent.com/1888355/230709515-163918d7-5ef0-47b7-b394-77b18c0c3d54.gif)
```swift
struct Joystick: View {/**
???
Use just State instead of GestureState to trigger animation on gesture ended.
This approach is right?refs:
https://stackoverflow.com/questions/72880712/animate-gesturestate-on-reset
*/
@State private var position: CGSize = .zero@GestureVelocity private var velocity: CGVector
var body: some View {
stick
.padding(10)
}private var stick: some View {
Circle()
.fill(Color.blue)
.frame(width: 100, height: 100)
.animatableOffset(position) // https://github.com/FluidGroup/swiftui-support
.gesture(
DragGesture(
minimumDistance: 0,
coordinateSpace: .local
)
.onChanged({ value in
withAnimation(.interactiveSpring()) {
position = value.translation
}
})
.onEnded({ value inlet distance = CGSize(
width: -position.width,
height: -position.height
)let mappedVelocity = CGVector(
dx: velocity.dx / distance.width,
dy: velocity.dy / distance.height
)
withAnimation(
.interpolatingSpring(
stiffness: 50,
damping: 10,
initialVelocity: mappedVelocity.dx
)
) {
position.width = 0
}
withAnimation(
.interpolatingSpring(
stiffness: 50,
damping: 10,
initialVelocity: mappedVelocity.dy
)
) {
position.height = 0
}
})
.updatingVelocity($velocity))
}
}
```## Example
```swift
RoundedRectangle(cornerRadius: 16, style: .continuous)
.fill(Color.blue)
.frame(width: 100, height: 100)
.modifier(VerticalDragModifier())
``````swift
struct VerticalDragModifier: ViewModifier {
/**
???
Use just State instead of GestureState to trigger animation on gesture ended.
This approach is right?
refs:
https://stackoverflow.com/questions/72880712/animate-gesturestate-on-reset
*/
@State private var position: CGSize = .zero
@GestureVelocity private var velocity: CGVector
func body(content: Content) -> some View {
content
.offset(position)
.gesture(
DragGesture(
minimumDistance: 0,
coordinateSpace: .local
)
.onChanged({ value in
withAnimation(.interactiveSpring()) {
position.height = value.translation.height
}
})
.onEnded({ value in
let distance = CGSize(
width: -position.width,
height: -position.height
)
let mappedVelocity = CGVector(
dx: velocity.dx / distance.width,
dy: velocity.dy / distance.height
)
withAnimation(
.interpolatingSpring(
stiffness: 50,
damping: 10,
initialVelocity: mappedVelocity.dy
)
) {
position.width = 0
position.height = 0
}
})
.updatingVelocity($velocity)
)
}
}
```