Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation Tracking over minimum distance #11

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions Sources/SwiftUISnapDraggingModifier/SnapDraggingModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public struct SnapDraggingModifier: ViewModifier {

@GestureState private var isTracking = false
@GestureState private var pointInView: CGPoint = .zero
@GestureState private var paddingTranslation: CGSize = .zero

@State private var isActive = false
@State private var contentSize: CGSize = .zero
Expand Down Expand Up @@ -159,7 +160,9 @@ public struct SnapDraggingModifier: ViewModifier {
}
}

let addingGesture = dragGesture.simultaneously(with: gesture)
let addingGesture = dragGesture
.simultaneously(with: trackingGesture)
.simultaneously(with: grabbingPointGesture)

Group {
switch gestureMode {
Expand Down Expand Up @@ -253,8 +256,11 @@ public struct SnapDraggingModifier: ViewModifier {

}

private var gesture: some Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .named(_CoordinateSpaceTag.pointInView))
private var grabbingPointGesture: some Gesture {
DragGesture(
minimumDistance: 0,
coordinateSpace: .named(_CoordinateSpaceTag.pointInView)
)
.updating(
$pointInView,
body: { v, s, _ in
Expand All @@ -263,6 +269,23 @@ public struct SnapDraggingModifier: ViewModifier {
)
}

private var trackingGesture: some Gesture {
DragGesture(
minimumDistance: 0,
coordinateSpace: .named(_CoordinateSpaceTag.transition)
)
.updating($paddingTranslation, body: { value, state, _ in
let reservedTranslation = CGSize(
width: (value.location.x - pointInView.x),
height: (value.location.y - pointInView.y)
)

if self.isActive == false {
state = reservedTranslation
}
})
}

private var dragGesture: some Gesture {
DragGesture(
minimumDistance: activation.minimumDistance,
Expand All @@ -280,13 +303,18 @@ public struct SnapDraggingModifier: ViewModifier {

self.isActive = true

// TODO: including minimumDistance
// TODO: including minimumDistance

let resolvedTranslation = CGSize(
var resolvedTranslation = CGSize(
width: (value.location.x - pointInView.x),
height: (value.location.y - pointInView.y)
)

resolvedTranslation.width -= paddingTranslation.width
resolvedTranslation.height -= paddingTranslation.height

print(paddingTranslation,resolvedTranslation)

// TODO: stop the current animation when dragging restarted.
withAnimation(.interactiveSpring()) {
if axis.contains(.horizontal) {
Expand Down Expand Up @@ -452,8 +480,9 @@ struct Joystick: View {
.fill(Color.yellow)
.frame(width: 100, height: 100)
.modifier(
SnapDraggingModifier(
SnapDraggingModifier(
offset: $offset,
activation: .init(minimumDistance: 50),
springParameter: .interpolation(mass: 1, stiffness: 1, damping: 1)
)
)
Expand Down Expand Up @@ -481,6 +510,7 @@ struct SwipeAction: View {
.modifier(
SnapDraggingModifier(
offset: $offset,
activation: .init(minimumDistance: 10),
axis: .horizontal,
horizontalBoundary: .init(min: 0, max: .infinity, bandLength: 50),
springParameter: .interpolation(mass: 1, stiffness: 100, damping: 10),
Expand Down
Loading