Created
February 11, 2020 03:51
-
-
Save dfrobison/85984334b0518a2dd6e0cec170a534a9 to your computer and use it in GitHub Desktop.
[Dragging View] from https://forums.developer.apple.com/thread/127002
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
import SwiftUI | |
struct DayView: View { | |
@GestureState var dragState = DragState.inactive | |
@State var viewDragState = CGSize(width: 0, height: 120) | |
var translationOffset: CGSize { | |
return CGSize(width: 0, height: viewDragState.height + dragState.translation.height) | |
} | |
enum DragState { | |
case inactive | |
case dragging(translation: CGSize) | |
var translation: CGSize { | |
switch self { | |
case .dragging(let translation): | |
return translation | |
default: | |
return .zero | |
} | |
} | |
} | |
var body: some View { | |
let dragGesture = DragGesture() | |
.updating($dragState) { value, state, transaction in | |
state = .dragging(translation: value.translation) | |
} | |
.onEnded { value in | |
self.viewDragState.height += value.translation.height | |
self.viewDragState.width += value.translation.width | |
} | |
return GeometryReader { geometry in | |
ZStack(alignment: .top) { | |
VStack(spacing: 0) { | |
ForEach(0..<2) {_ in | |
Text("X").frame( height: 60) | |
Divider() | |
} | |
} | |
Text("Meet with Test 2-4").frame(width: geometry.size.width, height: 60, alignment: .topLeading).background(Color.red) | |
.offset(self.translationOffset) | |
.gesture(dragGesture) | |
} | |
} | |
} | |
} | |
struct ContentView: View { | |
var body: some View { | |
HStack { | |
DayView().background(Color.blue) | |
DayView().background(Color.yellow) | |
DayView().background(Color.green) | |
}.padding(10).border(Color.red, width: 3) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment