Skip to content

Commit

Permalink
Add an option to enable/disable Swipe gesture (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
antranapp authored Dec 8, 2021
1 parent 19b8969 commit d455293
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Example/Shared/TwitterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct TwitterView: View {
@ObservedObject var sixthModel = LikesModel()

var body: some View {
PagerTabStripView(selection: $selection) {
PagerTabStripView(swipeGestureEnabled: false, selection: $selection) {
PostsList(isLoading: $firstModel.isLoading, items: firstModel.posts).pagerTabItem {
TwitterNavBarItem(title: "First big width")
}
Expand Down
19 changes: 14 additions & 5 deletions Sources/PagerTabStripView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public struct PagerTabStripView<Content>: View where Content: View {
@State private var selectionState = 0
@StateObject private var settings: PagerSettings
private var useBinding: Bool

public init(selection: Binding<Int>? = nil,
private let swipeGestureEnabled: Bool

public init(swipeGestureEnabled: Bool = true,
selection: Binding<Int>? = nil,
@ViewBuilder content: @escaping () -> Content) {
self.content = content
if let selection = selection {
Expand All @@ -30,11 +32,12 @@ public struct PagerTabStripView<Content>: View where Content: View {
useBinding = false
self._selectionBiding = .constant(0)
}
self.swipeGestureEnabled = swipeGestureEnabled
self._settings = StateObject(wrappedValue: PagerSettings())
}

public var body: some View {
WrapperPagerTabStripView(selection: useBinding ? $selectionBiding : $selectionState, content: content)
WrapperPagerTabStripView(swipeGestureEnabled: swipeGestureEnabled, selection: useBinding ? $selectionBiding : $selectionState, content: content)
.environmentObject(self.settings)
}
}
Expand All @@ -61,8 +64,12 @@ private struct WrapperPagerTabStripView<Content>: View where Content: View {
}
@GestureState private var translation: CGFloat = 0

public init(selection: Binding<Int>,
private let swipeGestureEnabled: Bool

public init(swipeGestureEnabled: Bool = true,
selection: Binding<Int>,
@ViewBuilder content: @escaping () -> Content) {
self.swipeGestureEnabled = swipeGestureEnabled
self.content = content
self._selection = selection
}
Expand All @@ -80,7 +87,8 @@ private struct WrapperPagerTabStripView<Content>: View where Content: View {
.animation(.interactiveSpring(response: 0.15, dampingFraction: 0.86, blendDuration: 0.25), value: translation)
.gesture(
DragGesture().updating(self.$translation) { value, state, _ in
if selection == 0 && value.translation.width > 0 {
guard swipeGestureEnabled else { return }
if (selection == 0 && value.translation.width > 0) {
let valueWidth = value.translation.width
let normTrans = valueWidth / (gproxy.size.width + 50)
let logValue = log(1 + normTrans)
Expand All @@ -94,6 +102,7 @@ private struct WrapperPagerTabStripView<Content>: View where Content: View {
state = value.translation.width
}
}.onEnded { value in
guard swipeGestureEnabled else { return }
let offset = value.predictedEndTranslation.width / gproxy.size.width
let newPredictedIndex = (CGFloat(selection) - offset).rounded()
let newIndex = min(max(Int(newPredictedIndex), 0), dataStore.itemsCount - 1)
Expand Down

0 comments on commit d455293

Please sign in to comment.