Tweak movement gesture

This commit is contained in:
Shadowfacts 2019-10-15 17:26:06 -04:00
parent f844d5466f
commit c948ecd587
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 11 additions and 18 deletions

View File

@ -17,7 +17,7 @@ struct ContentView: View {
return c
}()
@State var timer: Timer?
@State var prevHorizTranslation: CGFloat?
@State var initialXPosition: Int?
var body: some View {
GeometryReader { (geometry) in
@ -27,7 +27,7 @@ struct ContentView: View {
.onAppear(perform: self.startTimer)
.onDisappear(perform: self.stopTimer)
.onTapGesture(perform: self.onTap)
.gesture(self.horizDragGesture(geometry: geometry))
.gesture(self.horizDragGesture(geometry: geometry).simultaneously(with: self.verticalDragGesture))
// .gesture(ExclusiveGesture(horizDragGesture, verticalDragGesture))
// .gesture(horizDragGesture.simultaneously(with: verticalDragGesture))
@ -57,32 +57,25 @@ struct ContentView: View {
DragGesture(coordinateSpace: .global)
.onChanged { (state) in
guard let currentPiece = self.controller.currentPiece else { return }
// let position = (state.location.x) / geometry.size.width * 10
// let moved = currentPiece.moved(by: (Int(position.rounded()) - currentPiece.topLeft.0, 0))
// if !self.controller.overlapsAny(moved) {
// self.controller.currentPiece = moved
// }
if self.prevHorizTranslation == nil {
self.prevHorizTranslation = state.translation.width
if self.initialXPosition == nil {
self.initialXPosition = currentPiece.topLeft.0
}
let delta = state.translation.width - self.prevHorizTranslation!
let amount = Int((delta / 20).rounded())
let moved = currentPiece.moved(by: (amount, 0))
var moved = currentPiece
let xPosition = self.initialXPosition! + Int((state.translation.width / (geometry.size.width / 10)).rounded())
moved.topLeft = (xPosition, currentPiece.topLeft.1)
if !self.controller.overlapsAny(moved) {
self.controller.currentPiece = moved
self.prevHorizTranslation = state.translation.width
}
}.onEnded { (state) in
self.prevHorizTranslation = nil
self.initialXPosition = nil
}
}
var verticalDragGesture: some Gesture {
DragGesture()
.onEnded { (state) in
let deltaY = state.location.y - state.startLocation.y
if abs(deltaY) > 40 {
if deltaY > 0 {
if abs(state.translation.height) > 80 {
if state.translation.height > 0 {
self.onSwipeDown()
} else {
self.onSwipeUp()

View File

@ -10,7 +10,7 @@ import Foundation
public struct GamePiece {
public let tetromino: Tetromino
public internal(set) var topLeft: (Int, Int)
public var topLeft: (Int, Int)
public internal(set) var tiles: [[Bool]]
public init(tetromino: Tetromino) {