Skip to content

Commit

Permalink
Merge pull request #16 from twodayslate/initial_offset
Browse files Browse the repository at this point in the history
Add support for an initial offset
  • Loading branch information
fatbobman authored Oct 22, 2022
2 parents 636a095 + 36fd5df commit 7d42b5f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
Expand Down
5 changes: 4 additions & 1 deletion Demo/Demo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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") })
Expand Down
12 changes: 10 additions & 2 deletions Sources/SwipeCell/SwipeCellViewModifier1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions Sources/SwipeCell/SwipeCellViewModifier2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwipeCell/SwipeCellViewModifier3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions Sources/SwipeCell/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,7 +32,9 @@ extension View {
leftSlot: leftSlot,
rightSlot: rightSlot,
swipeCellStyle: swipeCellStyle,
clip: clip
clip: clip,
initialOffset: initalOffset,
initialOffsetResetDelay: initialOffsetResetDelay
)
)

Expand Down

0 comments on commit 7d42b5f

Please sign in to comment.