working better

fix_swipe
saint 2024-06-08 18:53:23 -04:00
parent ed1377d9c5
commit f23cff87ab
1 changed files with 108 additions and 49 deletions

View File

@ -243,6 +243,21 @@ struct ContentView: View {
@State var dragOffset = CGFloat(0) @State var dragOffset = CGFloat(0)
enum SwipeStartState {
case center
case right
case left
}
enum SwipeStartDir {
case left
case right
}
// @State var curSwipeState: SwipeState = .start
@State var startSwipeState: SwipeStartState?
@State var startSwipeDir: SwipeStartDir?
@Environment(\.appDatabase) private var appDatabase @Environment(\.appDatabase) private var appDatabase
@Query(RibbonRequest()) private var ribbons: [Ribbon] @Query(RibbonRequest()) private var ribbons: [Ribbon]
@ -351,77 +366,116 @@ struct ContentView: View {
} }
.offset(x: 20, y: 0) .offset(x: 20, y: 0)
.offset(x: pulledOut.width) .offset(x: pulledOut.width)
.offset(x: mainSwipe.width, y: mainSwipe.height) .offset(x: mainSwipe.width)
.gesture( .gesture(
DragGesture() DragGesture()
.onChanged { gesture in .onChanged { value in
let margin = CGFloat(30) // Calculate the offset
let rightSwipeLimit = CGFloat(110)
let leftSwipeLimit = CGFloat(-200)
print("mao mainSwipe :\(mainSwipe.width)") var newOffset = value.translation.width
print(pulledOut.width) if startSwipeState == nil {
if pulledOut.width == 0 {
startSwipeState = .center
var swipeWidth = gesture.translation.width } else if pulledOut.width < 0 {
var swipeActual = gesture.translation.width + pulledOut.width startSwipeState = .left
} else {
// swiping right startSwipeState = .right
if swipeWidth > 0 {
if swipeActual > rightSwipeLimit {
swipeWidth = rightSwipeLimit + pow(swipeActual - rightSwipeLimit, 0.8)
} }
mainSwipe.width = swipeWidth - pulledOut.width print("start swipe meow: \(startSwipeState)")
if newOffset > 0 {
// swiping right startSwipeDir = .right
} else { } else {
if swipeActual < leftSwipeLimit { startSwipeDir = .left
swipeWidth = leftSwipeLimit - pow(-swipeActual + leftSwipeLimit, 0.8)
} }
mainSwipe.width = swipeWidth - pulledOut.width
} }
// Apply resistance if out of bounds
let maxOffsetRight: CGFloat = 150
let maxOffsetLeft: CGFloat = 200
if newOffset < -maxOffsetLeft {
newOffset = -maxOffsetLeft + rubberBandEffect(newOffset + maxOffsetLeft)
} else if newOffset > maxOffsetRight {
newOffset = maxOffsetRight + rubberBandEffect(newOffset - maxOffsetRight)
}
// if newOffset.height < -maxOffset {
// newOffset.height = -maxOffset + rubberBandEffect(newOffset.height + maxOffset)
// } else if newOffset.height > maxOffset {
// newOffset.height = maxOffset + rubberBandEffect(newOffset.height - maxOffset)
// }
self.mainSwipe.width = newOffset
// dragOffset is what is used to make the text be readable // dragOffset is what is used to make the text be readable
// with the right pane being visible // with the right pane being visible
// if mainSwipe.width < -margin && pulledOut.width <= 0 { // if mainSwipe.width < -margin && pulledOut.width <= 0 {
if mainSwipe.width < -margin && pulledOut.width <= 0 {
dragOffset = margin + mainSwipe.width + pulledOut.width
}
if mainSwipe.width > 0 && pulledOut.width < 0 { // if mainSwipe.width < -margin && pulledOut.width <= 0 {
dragOffset = margin + mainSwipe.width + pulledOut.width // dragOffset = margin + mainSwipe.width + pulledOut.width
} // }
// if mainSwipe.width > 0 && pulledOut.width < 0 {
// dragOffset = margin + mainSwipe.width + pulledOut.width
// }
} }
.onEnded { _ in .onEnded { _ in
let pulledOutRight = CGFloat(-200) var finalSwipe = CGFloat(0)
let pulledOutLeft = CGFloat(110) let swipeLeftFinal = CGFloat(-200)
let margin = CGFloat(30) let swipeRightFinal = CGFloat(110)
// let margin = CGFloat(30)
var setPulledOutWith = CGFloat(0) if startSwipeState == .center {
var setDragOffset = CGFloat(0) if startSwipeDir == .right {
finalSwipe = swipeRightFinal
} else {
finalSwipe = swipeLeftFinal
}
} else if startSwipeState == .right {
finalSwipe = .zero
if startSwipeDir == .left {
finalSwipe = .zero
} else {
finalSwipe = swipeRightFinal
}
} else if startSwipeState == .left {
if mainSwipe.width < 0 && pulledOut.width > 0 { if startSwipeDir == .left {
setPulledOutWith = CGFloat(0) finalSwipe = swipeLeftFinal
} else if mainSwipe.width > 0 && pulledOut.width < 0 { } else {
setPulledOutWith = CGFloat(0) finalSwipe = .zero
}
} else if (mainSwipe.width < 0 && pulledOut.width < 0) ||
(mainSwipe.width < 0 && pulledOut.width == 0) {
setPulledOutWith = pulledOutRight
setDragOffset = margin + setPulledOutWith
} else if abs(mainSwipe.width + pulledOut.width) > 30 {
setPulledOutWith = pulledOutLeft
} }
startSwipeState = nil
startSwipeDir = nil
// var setDragOffset = CGFloat(0)
// if mainSwipe.width < 0 && pulledOut.width > 0 {
// setPulledOutWith = CGFloat(0)
// } else if mainSwipe.width > 0 && pulledOut.width < 0 {
// setPulledOutWith = CGFloat(0)
// } else if (mainSwipe.width < 0 && pulledOut.width < 0) ||
// (mainSwipe.width < 0 && pulledOut.width == 0) {
// setPulledOutWith = pulledOutRight
// setDragOffset = margin + setPulledOutWith
// } else if abs(mainSwipe.width + pulledOut.width) > 30 {
// setPulledOutWith = pulledOutLeft
// }
withAnimation(.spring(response: 0.2)) { withAnimation(.spring(response: 0.2)) {
pulledOut.width = setPulledOutWith pulledOut.width = finalSwipe
dragOffset = setDragOffset // dragOffset = setDragOffset
mainSwipe = .zero mainSwipe = .zero
} }
} }
) )
@ -430,6 +484,11 @@ struct ContentView: View {
.background(Color(red: 0.1, green: 0.1, blue: 0.1)) .background(Color(red: 0.1, green: 0.1, blue: 0.1))
} }
func handleVisibilityChanged2(_: String, change _: VisibilityChange, tracker _: VisibilityTracker<String>) {} func handleVisibilityChanged2(_: String, change _: VisibilityChange, tracker _: VisibilityTracker<String>) {}
func rubberBandEffect(_ offset: CGFloat) -> CGFloat {
let resistance: CGFloat = 0.55
return resistance * pow(abs(offset), 0.7) * (offset < 0 ? -1 : 1)
}
} }
private let itemFormatter: DateFormatter = { private let itemFormatter: DateFormatter = {