Created
July 2, 2019 06:27
-
-
Save grrrlikestaquitos/8c31a3452c3ea18bfe7cd2aab8b8b959 to your computer and use it in GitHub Desktop.
SwiftUI - DraggableView, Reusable Component
This file contains 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
// 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