Skip to content

Commit

Permalink
Fix: Finishing pagination if new touch is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoya committed Mar 29, 2022
1 parent 19159aa commit be3f03b
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 216 deletions.
4 changes: 0 additions & 4 deletions Example/SwiftUIPagerExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
6B4EC8A6240D0918001E7490 /* EmbeddedExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B4EC8A5240D0918001E7490 /* EmbeddedExampleView.swift */; };
6B4EC8A8240D1182001E7490 /* BizarreExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B4EC8A7240D1182001E7490 /* BizarreExampleView.swift */; };
6B6FAA3D24D553C8000D1539 /* PagingAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B6FAA3C24D553C8000D1539 /* PagingAnimation.swift */; };
6B9C4A8A24B45F66004C06C5 /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B9C4A8924B45F66004C06C5 /* OnDeactivateModifier.swift */; };
6BB1AAD524C9CA1C0032B5A3 /* PagerContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BB1AAD424C9CA1C0032B5A3 /* PagerContent.swift */; };
6BB1AAD724C9D0820032B5A3 /* Pager+Buildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BB1AAD624C9D0820032B5A3 /* Pager+Buildable.swift */; };
6BC5EDFC24866D9500E1E78C /* PagerContent+Buildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BC5EDF424866D9500E1E78C /* PagerContent+Buildable.swift */; };
Expand Down Expand Up @@ -71,7 +70,6 @@
6B4EC8A5240D0918001E7490 /* EmbeddedExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmbeddedExampleView.swift; sourceTree = "<group>"; };
6B4EC8A7240D1182001E7490 /* BizarreExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BizarreExampleView.swift; sourceTree = "<group>"; };
6B6FAA3C24D553C8000D1539 /* PagingAnimation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = PagingAnimation.swift; path = ../../Sources/SwiftUIPager/PageConfiguration/PagingAnimation.swift; sourceTree = "<group>"; };
6B9C4A8924B45F66004C06C5 /* OnDeactivateModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnDeactivateModifier.swift; sourceTree = "<group>"; };
6BB1AAD424C9CA1C0032B5A3 /* PagerContent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = PagerContent.swift; path = ../../Sources/SwiftUIPager/PagerContent.swift; sourceTree = "<group>"; };
6BB1AAD624C9D0820032B5A3 /* Pager+Buildable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "Pager+Buildable.swift"; path = "../../Sources/SwiftUIPager/Pager+Buildable.swift"; sourceTree = "<group>"; };
6BC5EDF424866D9500E1E78C /* PagerContent+Buildable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "PagerContent+Buildable.swift"; path = "../../Sources/SwiftUIPager/PagerContent+Buildable.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -174,7 +172,6 @@
6BC5EDF824866D9500E1E78C /* Buildable.swift */,
6BD3828124C97DE3007B1CF6 /* CGPoint+Angle.swift */,
17C41EA925B21D9A006F9EC4 /* OnAnimationCompletedModifier.swift */,
6B9C4A8924B45F66004C06C5 /* OnDeactivateModifier.swift */,
6BC5EDF724866D9500E1E78C /* View+Helper.swift */,
);
name = Helpers;
Expand Down Expand Up @@ -282,7 +279,6 @@
17C41EAA25B21D9A006F9EC4 /* OnAnimationCompletedModifier.swift in Sources */,
6BC5EDFF24866D9500E1E78C /* Buildable.swift in Sources */,
1748E8C026695E220016F534 /* PageTransition.swift in Sources */,
6B9C4A8A24B45F66004C06C5 /* OnDeactivateModifier.swift in Sources */,
6BEA731624ACF8D7007EA8DC /* GesturePriority.swift in Sources */,
6B35B6C125346610000D618F /* PaginationSensitivity.swift in Sources */,
1751176A2573D93F00D809CF /* Page.swift in Sources */,
Expand Down
39 changes: 0 additions & 39 deletions Sources/SwiftUIPager/Helpers/OnDeactivateModifier.swift

This file was deleted.

37 changes: 24 additions & 13 deletions Sources/SwiftUIPager/PagerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ extension Pager {
/// Page index
@ObservedObject var pagerModel: Page

#if !os(tvOS)
/// DragGesture state to indicate whether the gesture was interrupted
@GestureState var isGestureFinished = true
#endif

/// Initializes a new `Pager`.
///
/// - Parameter size: Available size
Expand Down Expand Up @@ -197,16 +202,10 @@ extension Pager {
#endif

var resultView = wrappedView
.rotation3DEffect((isHorizontal ? .zero : Angle(degrees: 90)) + scrollDirectionAngle,
axis: (0, 0, 1))
.onDeactivate(perform: {
if self.isDragging {
#if !os(tvOS)
self.onDragCancelled()
#endif
}
})
.eraseToAny()
.rotation3DEffect(
(isHorizontal ? .zero : Angle(degrees: 90)) + scrollDirectionAngle,
axis: (0, 0, 1)
).eraseToAny()

if #available(iOS 13.2, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
resultView = resultView
Expand All @@ -221,6 +220,18 @@ extension Pager {
.eraseToAny()
}

#if !os(tvOS)
if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) {
resultView = resultView
.onChange(of: isGestureFinished) { value in
if value {
onDragGestureEnded()
}
}
.eraseToAny()
}
#endif

return resultView.contentShape(Rectangle())
}
}
Expand Down Expand Up @@ -251,12 +262,12 @@ extension Pager.PagerContent {
#if !os(tvOS)
var swipeGesture: some Gesture {
DragGesture(minimumDistance: minimumDistance, coordinateSpace: .global)
.updating($isGestureFinished) { _, state, _ in
state = false
}
.onChanged({ value in
self.onDragChanged(with: value)
})
.onEnded({ (value) in
self.onDragGestureEnded()
})
}

func onDragChanged(with value: DragGesture.Value) {
Expand Down
12 changes: 0 additions & 12 deletions SwiftUIPager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
17C7B2682743CB3300BC51D4 /* Pager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B2482743CB3300BC51D4 /* Pager.swift */; };
17C7B2692743CB3300BC51D4 /* Pager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B2482743CB3300BC51D4 /* Pager.swift */; };
17C7B26A2743CB3300BC51D4 /* Pager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B2482743CB3300BC51D4 /* Pager.swift */; };
17C7B26B2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B24A2743CB3300BC51D4 /* OnDeactivateModifier.swift */; };
17C7B26C2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B24A2743CB3300BC51D4 /* OnDeactivateModifier.swift */; };
17C7B26D2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B24A2743CB3300BC51D4 /* OnDeactivateModifier.swift */; };
17C7B26E2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B24A2743CB3300BC51D4 /* OnDeactivateModifier.swift */; };
17C7B26F2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B24A2743CB3300BC51D4 /* OnDeactivateModifier.swift */; };
17C7B2702743CB3300BC51D4 /* OnAnimationCompletedModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B24B2743CB3300BC51D4 /* OnAnimationCompletedModifier.swift */; };
17C7B2712743CB3300BC51D4 /* OnAnimationCompletedModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B24B2743CB3300BC51D4 /* OnAnimationCompletedModifier.swift */; };
17C7B2722743CB3300BC51D4 /* OnAnimationCompletedModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17C7B24B2743CB3300BC51D4 /* OnAnimationCompletedModifier.swift */; };
Expand Down Expand Up @@ -153,7 +148,6 @@
17C7B2462743CB3300BC51D4 /* PagerContent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PagerContent.swift; sourceTree = "<group>"; };
17C7B2472743CB3300BC51D4 /* PageTransition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PageTransition.swift; sourceTree = "<group>"; };
17C7B2482743CB3300BC51D4 /* Pager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Pager.swift; sourceTree = "<group>"; };
17C7B24A2743CB3300BC51D4 /* OnDeactivateModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OnDeactivateModifier.swift; sourceTree = "<group>"; };
17C7B24B2743CB3300BC51D4 /* OnAnimationCompletedModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OnAnimationCompletedModifier.swift; sourceTree = "<group>"; };
17C7B24C2743CB3300BC51D4 /* View+Helper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+Helper.swift"; sourceTree = "<group>"; };
17C7B24D2743CB3300BC51D4 /* CGPoint+Angle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CGPoint+Angle.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -242,7 +236,6 @@
17C7B2492743CB3300BC51D4 /* Helpers */ = {
isa = PBXGroup;
children = (
17C7B24A2743CB3300BC51D4 /* OnDeactivateModifier.swift */,
17C7B24B2743CB3300BC51D4 /* OnAnimationCompletedModifier.swift */,
17C7B24C2743CB3300BC51D4 /* View+Helper.swift */,
17C7B24D2743CB3300BC51D4 /* CGPoint+Angle.swift */,
Expand Down Expand Up @@ -696,7 +689,6 @@
17C7B2B32743CB3300BC51D4 /* Page.swift in Sources */,
17C7B2812743CB3300BC51D4 /* Buildable.swift in Sources */,
17C7B2902743CB3300BC51D4 /* SwipeInteractionArea.swift in Sources */,
17C7B26D2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -723,7 +715,6 @@
17C7B2B42743CB3300BC51D4 /* Page.swift in Sources */,
17C7B2822743CB3300BC51D4 /* Buildable.swift in Sources */,
17C7B2912743CB3300BC51D4 /* SwipeInteractionArea.swift in Sources */,
17C7B26E2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -750,7 +741,6 @@
17C7B2B22743CB3300BC51D4 /* Page.swift in Sources */,
17C7B2802743CB3300BC51D4 /* Buildable.swift in Sources */,
17C7B28F2743CB3300BC51D4 /* SwipeInteractionArea.swift in Sources */,
17C7B26C2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -777,7 +767,6 @@
17C7B2B52743CB3300BC51D4 /* Page.swift in Sources */,
17C7B2832743CB3300BC51D4 /* Buildable.swift in Sources */,
17C7B2922743CB3300BC51D4 /* SwipeInteractionArea.swift in Sources */,
17C7B26F2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -804,7 +793,6 @@
17C7B2B12743CB3300BC51D4 /* Page.swift in Sources */,
17C7B27F2743CB3300BC51D4 /* Buildable.swift in Sources */,
17C7B28E2743CB3300BC51D4 /* SwipeInteractionArea.swift in Sources */,
17C7B26B2743CB3300BC51D4 /* OnDeactivateModifier.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
39 changes: 0 additions & 39 deletions SwiftUIPager/Helpers/OnDeactivateModifier.swift

This file was deleted.

1 change: 1 addition & 0 deletions SwiftUIPager/PagerContent+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extension Pager.PagerContent: Buildable {
}.flatMap { $0 }
}
self.pagerModel.isInfinite = value
self.pagerModel.totalPages = newData.count
return mutating(keyPath: \.isInifinitePager, value: value)
.mutating(keyPath: \.data, value: newData)
}
Expand Down
44 changes: 13 additions & 31 deletions SwiftUIPager/PagerContent+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,7 @@ extension Pager.PagerContent {
/// Oppacity for each item when `faded` animation is chosen
func opacity(for item: PageWrapper<Element, ID>) -> Double {
guard let opacityIncrement = opacityIncrement else { return 1 }
guard let index = data.firstIndex(of: item) else { return 1 }
let totalIncrement = abs(totalOffset / pageDistance)
let currentPage = direction == .forward ? CGFloat(page) + totalIncrement : CGFloat(page) - totalIncrement

let distance = abs(CGFloat(index) - currentPage)
let distance = abs(distance(to: item))
return Double(max(0, min(1, 1 - distance * CGFloat(opacityIncrement))))
}

Expand All @@ -264,17 +260,8 @@ extension Pager.PagerContent {
/// Angle for the 3D rotation effect
func angle(for item: PageWrapper<Element, ID>) -> Angle {
guard shouldRotate else { return .zero }
guard let index = data.firstIndex(of: item) else { return .zero }

let totalIncrement = abs(totalOffset / pageDistance)

let currentAngle = Angle(degrees: Double(page - index) * rotationDegrees)
guard isDragging else {
return currentAngle
}

let newAngle = direction == .forward ? Angle(degrees: currentAngle.degrees + rotationDegrees * Double(totalIncrement)) : Angle(degrees: currentAngle.degrees - rotationDegrees * Double(totalIncrement) )
return newAngle
let distance = distance(to: item)
return Angle(degrees: rotationDegrees * Double(distance))
}

/// Axis for the rotations effect
Expand All @@ -285,24 +272,19 @@ extension Pager.PagerContent {

/// Scale that applies to a particular item
func scale(for item: PageWrapper<Element, ID>) -> CGFloat {
guard isDragging else { return isFocused(item) ? 1 : interactiveScale }

let totalIncrement = abs(totalOffset / pageDistance)
let currentPage = direction == .forward ? CGFloat(page) + totalIncrement : CGFloat(page) - totalIncrement

guard let indexInt = data.firstIndex(of: item) else { return interactiveScale }
let distance = abs(distance(to: item))
return Double(max(interactiveScale, min(1, 1 - distance * scaleIncrement)))
}

let index = CGFloat(indexInt)
guard abs(currentPage - index) <= 1 else { return interactiveScale }
private func distance(to item: PageWrapper<Element, ID>) -> CGFloat {
guard let index: Int = dataDisplayed.firstIndex(of: item) else { return 0 }
guard let displayedItem = dataDisplayed.first(where: { $0 == data[page] }) else { return 0 }
guard let displayedIndex: Int = dataDisplayed.firstIndex(of: displayedItem) else { return 0 }

let increment = totalIncrement - totalIncrement.rounded(.towardZero)
let nextPage = direction == .forward ? currentPage.rounded(.awayFromZero) : currentPage.rounded(.towardZero)
guard currentPage > 0 else {
return 1 - (scaleIncrement * increment)
}
let totalIncrement = abs(totalOffset / pageDistance)
let currentIndex = direction == .forward ? CGFloat(index) - totalIncrement : CGFloat(index) + totalIncrement

return index == nextPage ? interactiveScale + (scaleIncrement * increment)
: 1 - (scaleIncrement * increment)
return CGFloat(displayedIndex) - currentIndex
}

/// Returns true if the item is focused on the screen.
Expand Down
Loading

0 comments on commit be3f03b

Please sign in to comment.