Compare commits

...

2 Commits

2 changed files with 60 additions and 2 deletions

View File

@ -0,0 +1,38 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"colors" : [
{
"idiom" : "universal",
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "1.000",
"alpha" : "1.000",
"blue" : "1.000",
"green" : "1.000"
}
}
},
{
"idiom" : "universal",
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "0.000",
"alpha" : "1.000",
"blue" : "0.000",
"green" : "0.000"
}
}
}
]
}

View File

@ -17,6 +17,7 @@ struct ContentView: View {
return c return c
}() }()
@State var timer: Timer? @State var timer: Timer?
@State var gestureState: GestureState = .none
@State var initialXPosition: Int? @State var initialXPosition: Int?
var body: some View { var body: some View {
@ -40,7 +41,6 @@ struct ContentView: View {
.onAppear(perform: self.startTimer) .onAppear(perform: self.startTimer)
.onDisappear(perform: self.stopTimer) .onDisappear(perform: self.stopTimer)
.onTapGesture(perform: self.onTap) .onTapGesture(perform: self.onTap)
.gesture(self.horizDragGesture(geometry: geometry).simultaneously(with: self.verticalDragGesture))
// .gesture(ExclusiveGesture(horizDragGesture, verticalDragGesture)) // .gesture(ExclusiveGesture(horizDragGesture, verticalDragGesture))
// .gesture(horizDragGesture.simultaneously(with: verticalDragGesture)) // .gesture(horizDragGesture.simultaneously(with: verticalDragGesture))
@ -50,6 +50,8 @@ struct ContentView: View {
} }
.padding(.trailing, 8) .padding(.trailing, 8)
} }
.background(Color("TempBackground")) // visually does nothing but lets the gesture work outside of the board, see FB7385742
.gesture(self.horizDragGesture(geometry: geometry).simultaneously(with: self.verticalDragGesture))
Spacer() Spacer()
HStack { HStack {
Button(action: { self.controller.rotate(direction: .counterClockwise) }) { Button(action: { self.controller.rotate(direction: .counterClockwise) }) {
@ -73,26 +75,40 @@ struct ContentView: View {
func horizDragGesture(geometry: GeometryProxy) -> some Gesture { func horizDragGesture(geometry: GeometryProxy) -> some Gesture {
DragGesture(coordinateSpace: .global) DragGesture(coordinateSpace: .global)
.onChanged { (state) in .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 } let currentPiece = self.controller.currentPiece else { return }
if self.initialXPosition == nil { if self.initialXPosition == nil {
self.initialXPosition = currentPiece.topLeft.0 self.initialXPosition = currentPiece.topLeft.0
} }
var moved = currentPiece var moved = currentPiece
let xPosition = self.initialXPosition! + Int((state.translation.width / (geometry.size.width / 10)).rounded()) 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) moved.topLeft = (xPosition, currentPiece.topLeft.1)
if !self.controller.overlapsAny(moved) { if !self.controller.overlapsAny(moved) {
self.controller.currentPiece = moved self.controller.currentPiece = moved
} }
}.onEnded { (state) in }.onEnded { (state) in
self.initialXPosition = nil self.initialXPosition = nil
if self.gestureState == .horizontal {
self.gestureState = .none
}
} }
} }
var verticalDragGesture: some Gesture { var verticalDragGesture: some Gesture {
DragGesture() DragGesture()
.onChanged({ (state) in
guard self.gestureState != .horizontal else { return }
})
.onEnded { (state) in .onEnded { (state) in
guard self.gestureState != .horizontal else { return }
if abs(state.translation.height) > 80 { if abs(state.translation.height) > 80 {
self.gestureState = .none
if state.translation.height > 0 { if state.translation.height > 0 {
self.onSwipeDown() self.onSwipeDown()
} else { } else {
@ -133,6 +149,10 @@ struct ContentView: View {
} }
} }
enum GestureState: Equatable {
case none, horizontal, vertical
}
struct ContentView_Previews: PreviewProvider { struct ContentView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
ContentView() ContentView()