Don't let horizontal and vertical gestures run simultaneously

This commit is contained in:
Shadowfacts 2019-10-16 17:27:33 -04:00
parent 3e7baecd27
commit 5acbf0f5c3
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 20 additions and 1 deletions

View File

@ -17,6 +17,7 @@ struct ContentView: View {
return c
}()
@State var timer: Timer?
@State var gestureState: GestureState = .none
@State var initialXPosition: Int?
var body: some View {
@ -73,26 +74,40 @@ struct ContentView: View {
func horizDragGesture(geometry: GeometryProxy) -> some Gesture {
DragGesture(coordinateSpace: .global)
.onChanged { (state) in
guard case .playing(.normal) = self.controller.state,
guard self.gestureState != .vertical,
case .playing(.normal) = self.controller.state,
let currentPiece = self.controller.currentPiece else { return }
if self.initialXPosition == nil {
self.initialXPosition = currentPiece.topLeft.0
}
var moved = currentPiece
let xPosition = self.initialXPosition! + Int((state.translation.width / (geometry.size.width / 10)).rounded())
if xPosition != self.initialXPosition {
self.gestureState = .horizontal
}
moved.topLeft = (xPosition, currentPiece.topLeft.1)
if !self.controller.overlapsAny(moved) {
self.controller.currentPiece = moved
}
}.onEnded { (state) in
self.initialXPosition = nil
if self.gestureState == .horizontal {
self.gestureState = .none
}
}
}
var verticalDragGesture: some Gesture {
DragGesture()
.onChanged({ (state) in
guard self.gestureState != .horizontal else { return }
})
.onEnded { (state) in
guard self.gestureState != .horizontal else { return }
if abs(state.translation.height) > 80 {
self.gestureState = .none
if state.translation.height > 0 {
self.onSwipeDown()
} else {
@ -133,6 +148,10 @@ struct ContentView: View {
}
}
enum GestureState: Equatable {
case none, horizontal, vertical
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()