working better
							parent
							
								
									ed1377d9c5
								
							
						
					
					
						commit
						f23cff87ab
					
				|  | @ -243,6 +243,21 @@ struct ContentView: View { | |||
| 
 | ||||
|     @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 | ||||
| 
 | ||||
|     @Query(RibbonRequest()) private var ribbons: [Ribbon] | ||||
|  | @ -351,76 +366,115 @@ struct ContentView: View { | |||
|                 } | ||||
|                 .offset(x: 20, y: 0) | ||||
|                 .offset(x: pulledOut.width) | ||||
|                 .offset(x: mainSwipe.width, y: mainSwipe.height) | ||||
|                 .offset(x: mainSwipe.width) | ||||
|                 .gesture( | ||||
|                     DragGesture() | ||||
|                         .onChanged { gesture in | ||||
|                             let margin = CGFloat(30) | ||||
|                             let rightSwipeLimit = CGFloat(110) | ||||
|                             let leftSwipeLimit = CGFloat(-200) | ||||
|                         .onChanged { value in | ||||
|                             // Calculate the offset | ||||
| 
 | ||||
|                             print("mao mainSwipe :\(mainSwipe.width)") | ||||
|                             print(pulledOut.width) | ||||
| 
 | ||||
| 
 | ||||
|                             var swipeWidth = gesture.translation.width | ||||
|                             var swipeActual = gesture.translation.width + pulledOut.width | ||||
| 
 | ||||
|                             // swiping right | ||||
|                             if swipeWidth > 0 { | ||||
|                                 if swipeActual > rightSwipeLimit { | ||||
|                                     swipeWidth = rightSwipeLimit + pow(swipeActual - rightSwipeLimit, 0.8) | ||||
|                                 } | ||||
| 
 | ||||
|                                 mainSwipe.width = swipeWidth - pulledOut.width | ||||
| 
 | ||||
|                             // swiping right | ||||
|                             var newOffset = value.translation.width | ||||
|                             if startSwipeState == nil { | ||||
|                                 if pulledOut.width == 0 { | ||||
|                                     startSwipeState = .center | ||||
|                                 } else if pulledOut.width < 0 { | ||||
|                                     startSwipeState = .left | ||||
|                                 } else { | ||||
|                                 if swipeActual < leftSwipeLimit { | ||||
|                                     swipeWidth = leftSwipeLimit - pow(-swipeActual + leftSwipeLimit, 0.8) | ||||
|                                     startSwipeState = .right | ||||
|                                 } | ||||
| 
 | ||||
|                                 mainSwipe.width = swipeWidth - pulledOut.width | ||||
|                                 print("start swipe meow: \(startSwipeState)") | ||||
|                                 if newOffset > 0 { | ||||
|                                     startSwipeDir = .right | ||||
|                                 } else { | ||||
|                                     startSwipeDir = .left | ||||
|                                 } | ||||
|                             } | ||||
| 
 | ||||
| 
 | ||||
|                             // 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 | ||||
|                             // with the right pane being visible | ||||
| 
 | ||||
|                             // 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 { | ||||
|                                 dragOffset = margin + mainSwipe.width + pulledOut.width | ||||
|                             } | ||||
|                             // if mainSwipe.width < -margin &&  pulledOut.width <= 0 { | ||||
|                             //     dragOffset = margin + mainSwipe.width + pulledOut.width | ||||
|                             // } | ||||
| 
 | ||||
|                             // if mainSwipe.width > 0 && pulledOut.width < 0 { | ||||
|                             //     dragOffset = margin + mainSwipe.width + pulledOut.width | ||||
|                             // } | ||||
|                         } | ||||
|                         .onEnded { _ in | ||||
|                             let pulledOutRight = CGFloat(-200) | ||||
|                             let pulledOutLeft = CGFloat(110) | ||||
|                             let margin = CGFloat(30) | ||||
|                             var finalSwipe = CGFloat(0) | ||||
|                             let swipeLeftFinal = CGFloat(-200) | ||||
|                             let swipeRightFinal = CGFloat(110) | ||||
|                             // let margin = CGFloat(30) | ||||
| 
 | ||||
|                             var setPulledOutWith = CGFloat(0) | ||||
|                             var setDragOffset = CGFloat(0) | ||||
|                             if startSwipeState == .center { | ||||
|                                 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 { | ||||
|                                 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 | ||||
|                                 if startSwipeDir == .left { | ||||
|                                     finalSwipe = swipeLeftFinal | ||||
|                                 } else { | ||||
|                                     finalSwipe = .zero | ||||
|                                 } | ||||
|                             } | ||||
| 
 | ||||
|                             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)) { | ||||
|                                 pulledOut.width = setPulledOutWith | ||||
|                                 dragOffset = setDragOffset | ||||
|                                 pulledOut.width = finalSwipe | ||||
|                                 // dragOffset = setDragOffset | ||||
|                                  mainSwipe = .zero | ||||
|                             } | ||||
|                         } | ||||
|  | @ -430,6 +484,11 @@ struct ContentView: View { | |||
|         .background(Color(red: 0.1, green: 0.1, blue: 0.1)) | ||||
|     } | ||||
|     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 = { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue