Skip to content

Instantly share code, notes, and snippets.

@grrrlikestaquitos
Created July 2, 2019 06:27
Show Gist options
  • Save grrrlikestaquitos/618fe2e2007872a79956f9674b4649d0 to your computer and use it in GitHub Desktop.
Save grrrlikestaquitos/618fe2e2007872a79956f9674b4649d0 to your computer and use it in GitHub Desktop.
SwiftUI - DraggableView, Reusable Component
// Creating a new HOC Component
struct DraggableView<C: View> : View {
@State private var drawerOffset = CGSize.zero
private let childView: C
init (_ childView: () -> (C)) {
self.childView = childView()
}
// Handling business logic for drag gesture and animations
var body: some View {
Group {
childView
.frame(width: 150, height: 150)
.animation(.spring())
}.offset(drawerOffset)
.gesture(DragGesture()
.onChanged({ drag in
self.drawerOffset = drag.translation
})
.onEnded({ drag in
self.drawerOffset = CGSize.zero
}))
}
}
// Using DraggableView by passing unique components
...body: some View {
HStack {
DraggableView {
Image("Face")
.resizable()
.clipShape(Circle())
}
DraggableView {
Text("Drag me!")
.font(.largeTitle)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment