Tweak movement gesture
This commit is contained in:
parent
f844d5466f
commit
c948ecd587
|
@ -17,7 +17,7 @@ struct ContentView: View {
|
||||||
return c
|
return c
|
||||||
}()
|
}()
|
||||||
@State var timer: Timer?
|
@State var timer: Timer?
|
||||||
@State var prevHorizTranslation: CGFloat?
|
@State var initialXPosition: Int?
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
GeometryReader { (geometry) in
|
GeometryReader { (geometry) in
|
||||||
|
@ -27,7 +27,7 @@ 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))
|
.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))
|
||||||
|
|
||||||
|
@ -57,32 +57,25 @@ struct ContentView: View {
|
||||||
DragGesture(coordinateSpace: .global)
|
DragGesture(coordinateSpace: .global)
|
||||||
.onChanged { (state) in
|
.onChanged { (state) in
|
||||||
guard let currentPiece = self.controller.currentPiece else { return }
|
guard let currentPiece = self.controller.currentPiece else { return }
|
||||||
// let position = (state.location.x) / geometry.size.width * 10
|
if self.initialXPosition == nil {
|
||||||
// let moved = currentPiece.moved(by: (Int(position.rounded()) - currentPiece.topLeft.0, 0))
|
self.initialXPosition = currentPiece.topLeft.0
|
||||||
// if !self.controller.overlapsAny(moved) {
|
|
||||||
// self.controller.currentPiece = moved
|
|
||||||
// }
|
|
||||||
if self.prevHorizTranslation == nil {
|
|
||||||
self.prevHorizTranslation = state.translation.width
|
|
||||||
}
|
}
|
||||||
let delta = state.translation.width - self.prevHorizTranslation!
|
var moved = currentPiece
|
||||||
let amount = Int((delta / 20).rounded())
|
let xPosition = self.initialXPosition! + Int((state.translation.width / (geometry.size.width / 10)).rounded())
|
||||||
let moved = currentPiece.moved(by: (amount, 0))
|
moved.topLeft = (xPosition, currentPiece.topLeft.1)
|
||||||
if !self.controller.overlapsAny(moved) {
|
if !self.controller.overlapsAny(moved) {
|
||||||
self.controller.currentPiece = moved
|
self.controller.currentPiece = moved
|
||||||
self.prevHorizTranslation = state.translation.width
|
|
||||||
}
|
}
|
||||||
}.onEnded { (state) in
|
}.onEnded { (state) in
|
||||||
self.prevHorizTranslation = nil
|
self.initialXPosition = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var verticalDragGesture: some Gesture {
|
var verticalDragGesture: some Gesture {
|
||||||
DragGesture()
|
DragGesture()
|
||||||
.onEnded { (state) in
|
.onEnded { (state) in
|
||||||
let deltaY = state.location.y - state.startLocation.y
|
if abs(state.translation.height) > 80 {
|
||||||
if abs(deltaY) > 40 {
|
if state.translation.height > 0 {
|
||||||
if deltaY > 0 {
|
|
||||||
self.onSwipeDown()
|
self.onSwipeDown()
|
||||||
} else {
|
} else {
|
||||||
self.onSwipeUp()
|
self.onSwipeUp()
|
||||||
|
|
|
@ -10,7 +10,7 @@ import Foundation
|
||||||
|
|
||||||
public struct GamePiece {
|
public struct GamePiece {
|
||||||
public let tetromino: Tetromino
|
public let tetromino: Tetromino
|
||||||
public internal(set) var topLeft: (Int, Int)
|
public var topLeft: (Int, Int)
|
||||||
public internal(set) var tiles: [[Bool]]
|
public internal(set) var tiles: [[Bool]]
|
||||||
|
|
||||||
public init(tetromino: Tetromino) {
|
public init(tetromino: Tetromino) {
|
||||||
|
|
Loading…
Reference in New Issue