diff --git a/Demo/Demo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Demo/Demo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 96d1e12..9a3598c 100644 --- a/Demo/Demo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Demo/Demo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/timbersoftware/SwiftUI-Introspect.git", "state": { "branch": null, - "revision": "de5c32c15ae169cfcb27397ffb2734dcd0e1e6d5", - "version": "0.1.0" + "revision": "f2616860a41f9d9932da412a8978fec79c06fe24", + "version": "0.1.4" } } ] diff --git a/Demo/Demo/ContentView.swift b/Demo/Demo/ContentView.swift index 0a7b64a..d7ef528 100644 --- a/Demo/Demo/ContentView.swift +++ b/Demo/Demo/ContentView.swift @@ -102,7 +102,7 @@ struct ContentView: View { Button(action: { print("button") }) { demo2() } - .swipeCell(cellPosition: .both, leftSlot: slot1, rightSlot: slot1) + .swipeCell(cellPosition: .both, leftSlot: slot1, rightSlot: slot1, initalOffset: 74 * 2, initialOffsetResetDelay: 2.0) demo3() .onTapGesture { @@ -168,6 +168,9 @@ struct ContentView: View { NavigationLink("ScrollView single Cell", destination: Demo8()) } .navigationBarTitle("SwipeCell Demo", displayMode: .inline) + .toolbar { + EditButton() + } } .dismissSwipeCell() .sheet(isPresented: $showSheet, content: { Text("Hello world") }) diff --git a/Sources/SwipeCell/SwipeCellViewModifier1.swift b/Sources/SwipeCell/SwipeCellViewModifier1.swift index b35fa5c..17732b3 100644 --- a/Sources/SwipeCell/SwipeCellViewModifier1.swift +++ b/Sources/SwipeCell/SwipeCellViewModifier1.swift @@ -15,11 +15,15 @@ struct SwipeCellModifier: ViewModifier { let rightSlot: SwipeCellSlot? let swipeCellStyle: SwipeCellStyle let clip: Bool + /// If the offset should be reset to 0 onAppaer + @State var resetOffsetOnAppear = true + /// The amount of time it should take to reset the offset to 0.0 on appear + let initialOffsetResetDelay: TimeInterval @State var status: CellStatus = .showCell @State var showDalayButtonWith: CGFloat = 0 - @State var offset: CGFloat = 0 + @State var offset: CGFloat @State var frameWidth: CGFloat = 99999 @State var leftOffset: CGFloat = -10000 @@ -60,13 +64,17 @@ struct SwipeCellModifier: ViewModifier { leftSlot: SwipeCellSlot?, rightSlot: SwipeCellSlot?, swipeCellStyle: SwipeCellStyle, - clip: Bool + clip: Bool, + initialOffset: CGFloat = 0.0, + initialOffsetResetDelay: TimeInterval = 0.0 ) { _cellPosition = State(wrappedValue: cellPosition) self.clip = clip self.leftSlot = leftSlot self.rightSlot = rightSlot self.swipeCellStyle = swipeCellStyle + self._offset = State(initialValue: initialOffset) + self.initialOffsetResetDelay = initialOffsetResetDelay } func buttonView(_ slot: SwipeCellSlot, _ i: Int) -> some View { diff --git a/Sources/SwipeCell/SwipeCellViewModifier2.swift b/Sources/SwipeCell/SwipeCellViewModifier2.swift index 0eca3f5..8c5b477 100644 --- a/Sources/SwipeCell/SwipeCellViewModifier2.swift +++ b/Sources/SwipeCell/SwipeCellViewModifier2.swift @@ -57,6 +57,13 @@ extension SwipeCellModifier { } .contentShape(Rectangle()) .gesture(getGesture()) + .onAppear { + withAnimation(Animation.default.delay(initialOffsetResetDelay)) { + if resetOffsetOnAppear { + offset = 0 + } + } + } .ifIs(clip) { $0.clipShape(Rectangle()) } diff --git a/Sources/SwipeCell/SwipeCellViewModifier3.swift b/Sources/SwipeCell/SwipeCellViewModifier3.swift index 7595c10..c144270 100644 --- a/Sources/SwipeCell/SwipeCellViewModifier3.swift +++ b/Sources/SwipeCell/SwipeCellViewModifier3.swift @@ -15,6 +15,9 @@ extension SwipeCellModifier { .onChanged { value in var width = value.translation.width cancellables.removeAll() //只要移动,定时清零 + + // A gesture happened so don't reset + self.resetOffsetOnAppear = false if currentCellID != cellID { currentCellID = cellID diff --git a/Sources/SwipeCell/ViewExtension.swift b/Sources/SwipeCell/ViewExtension.swift index 0afbc0a..40352ed 100644 --- a/Sources/SwipeCell/ViewExtension.swift +++ b/Sources/SwipeCell/ViewExtension.swift @@ -11,7 +11,9 @@ extension View { rightSlot: SwipeCellSlot?, swipeCellStyle: SwipeCellStyle = .defaultStyle(), clip: Bool = true, - disable: Bool = false + disable: Bool = false, + initalOffset: CGFloat = 0, + initialOffsetResetDelay: TimeInterval = 0.0 ) -> some View { var d = disable if cellPosition == .none { @@ -30,7 +32,9 @@ extension View { leftSlot: leftSlot, rightSlot: rightSlot, swipeCellStyle: swipeCellStyle, - clip: clip + clip: clip, + initialOffset: initalOffset, + initialOffsetResetDelay: initialOffsetResetDelay ) )