Last active
April 5, 2023 16:24
-
-
Save ohayon/83305d9c48e4fc3e7c3851ba10ddc4c9 to your computer and use it in GitHub Desktop.
Example of making a reusable `draggable()` modifier for SwiftUI Views
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
struct DraggablePita: View { | |
var body: some View { | |
Image(uiImage: UIImage(named: "pita.png")!) | |
.draggable() // Add the new, custom modifier to make this draggable | |
} | |
} | |
// Handle dragging | |
struct DraggableView: ViewModifier { | |
@State var offset = CGPoint(x: 0, y: 0) | |
func body(content: Content) -> some View { | |
content | |
.gesture(DragGesture(minimumDistance: 0) | |
.onChanged { value in | |
self.offset.x += value.location.x - value.startLocation.x | |
self.offset.y += value.location.y - value.startLocation.y | |
}) | |
.offset(x: offset.x, y: offset.y) | |
} | |
} | |
// Wrap `draggable()` in a View extension to have a clean call site | |
extension View { | |
func draggable() -> some View { | |
return modifier(DraggableView()) | |
} | |
} |
hey @lockieluke ! this code is almost 4 years old, i'm not surprised it doesnt work anymore 😄
import Foundation
import SwiftUI
import Throttler
struct DraggableView: ViewModifier {
@State private var offset = CGPoint(x: 0, y: 0)
@Binding private var allowDragging: Bool
private let onDrag: ((_ offset: CGPoint) -> Void)?
init(onDrag: ((_ offset: CGPoint) -> Void)? = nil, allowDragging: Binding<Bool> = .constant(true)) {
self.onDrag = onDrag
self._allowDragging = allowDragging
}
func body(content: Content) -> some View {
if allowDragging {
content.gesture(DragGesture(coordinateSpace: .global)
.onChanged { value in
self.offset.x = value.location.x - value.startLocation.x
self.offset.y = value.location.y - value.startLocation.y
self.onDrag?(self.offset)
}
.onEnded { value in
withAnimation(.interactiveSpring()) {
self.offset = .zero
}
}
)
.offset(x: offset.x, y: offset.y)
} else {
content
}
}
}
Fixed it but you'll have to use Throttler
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it freezes my app lol