Skip to content

Commit

Permalink
Merge pull request #72 from fermoya/fix/issue-69
Browse files Browse the repository at this point in the history
Fix/issue 69 Gesture state not resetting if canceled
  • Loading branch information
fermoya authored Jul 8, 2020
2 parents 4face44 + 93a1f26 commit c2218ba
Show file tree
Hide file tree
Showing 65 changed files with 337 additions and 140 deletions.
6 changes: 6 additions & 0 deletions Example/SwiftUIPagerExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
6B4EC8A4240D07D5001E7490 /* InfiniteExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B4EC8A3240D07D5001E7490 /* InfiniteExampleView.swift */; };
6B4EC8A6240D0918001E7490 /* EmbeddedExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B4EC8A5240D0918001E7490 /* EmbeddedExampleView.swift */; };
6B4EC8A8240D1182001E7490 /* BizarreExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B4EC8A7240D1182001E7490 /* BizarreExampleView.swift */; };
6B9C4A8A24B45F66004C06C5 /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B9C4A8924B45F66004C06C5 /* OnDeactivateModifier.swift */; };
6BC5EDFC24866D9500E1E78C /* Pager+Buildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BC5EDF424866D9500E1E78C /* Pager+Buildable.swift */; };
6BC5EDFE24866D9500E1E78C /* View+Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BC5EDF724866D9500E1E78C /* View+Helper.swift */; };
6BC5EDFF24866D9500E1E78C /* Buildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BC5EDF824866D9500E1E78C /* Buildable.swift */; };
Expand Down Expand Up @@ -57,6 +58,7 @@
6B4EC8A3240D07D5001E7490 /* InfiniteExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfiniteExampleView.swift; sourceTree = "<group>"; };
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>"; };
6B9C4A8924B45F66004C06C5 /* OnDeactivateModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnDeactivateModifier.swift; sourceTree = "<group>"; };
6BC5EDF424866D9500E1E78C /* Pager+Buildable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Pager+Buildable.swift"; path = "../../Sources/SwiftUIPager/Pager+Buildable.swift"; sourceTree = "<group>"; };
6BC5EDF724866D9500E1E78C /* View+Helper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+Helper.swift"; sourceTree = "<group>"; };
6BC5EDF824866D9500E1E78C /* Buildable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Buildable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -149,6 +151,7 @@
isa = PBXGroup;
children = (
6BC5EDF824866D9500E1E78C /* Buildable.swift */,
6B9C4A8924B45F66004C06C5 /* OnDeactivateModifier.swift */,
6BC5EDF924866D9500E1E78C /* SizeViewModifier.swift */,
6BC5EDF724866D9500E1E78C /* View+Helper.swift */,
);
Expand Down Expand Up @@ -250,6 +253,7 @@
6B4EC8A8240D1182001E7490 /* BizarreExampleView.swift in Sources */,
17D9E0F623D4CF6700C5AE93 /* SceneDelegate.swift in Sources */,
6BC5EDFF24866D9500E1E78C /* Buildable.swift in Sources */,
6B9C4A8A24B45F66004C06C5 /* OnDeactivateModifier.swift in Sources */,
6BEA731624ACF8D7007EA8DC /* GesturePriority.swift in Sources */,
6BEA731324ACF8D7007EA8DC /* PositionAlignment.swift in Sources */,
6BEA731424ACF8D7007EA8DC /* SwipeDirection.swift in Sources */,
Expand Down Expand Up @@ -402,6 +406,7 @@
DEVELOPMENT_TEAM = LPBB5C5CRP;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = SwiftUIPagerExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -422,6 +427,7 @@
DEVELOPMENT_TEAM = LPBB5C5CRP;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = SwiftUIPagerExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
37 changes: 37 additions & 0 deletions Sources/SwiftUIPager/Helpers/OnDeactivateModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// OnDeactivateModifier.swift
// SwiftUIPagerExample
//
// Created by Fernando Moya de Rivas on 07/07/2020.
// Copyright © 2020 Fernando Moya de Rivas. All rights reserved.
//

import SwiftUI

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
struct OnDeactivateView<Content: View>: View {

var content: Content
var perform: () -> Void

var body: some View {
#if os(iOS)
return content
.onReceive(NotificationCenter.default.publisher(for: UIScene.didActivateNotification), perform: { _ in
self.perform()
})
#else
return content
#endif
}

}

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension View {

func onDeactivate(perform: @escaping () -> Void) -> some View {
return OnDeactivateView(content: self, perform: perform)
}

}
62 changes: 45 additions & 17 deletions Sources/SwiftUIPager/Pager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,14 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
/// Size of the view
@State var size: CGSize = .zero

/// Translation on the X-Axis
/// `swipeGesture` translation on the X-Axis
@State var draggingOffset: CGFloat = 0

/// The moment when the dragging gesture started
@State var draggingStartTime: Date! = nil
/// `swipeGesture` velocity on the X-Axis
@State var draggingVelocity: Double = 0

/// Timestamp of the last `swipeGesture` entry
@State var draggingTimestamp: Date!

/// Page index
@Binding var pageIndex: Int {
Expand Down Expand Up @@ -179,6 +182,13 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
.onAppear(perform: {
self.onPageChanged?(self.page)
})
.onDeactivate(perform: {
if self.isDragging {
#if !os(tvOS)
self.onDragGestureEnded()
#endif
}
})
}

}
Expand Down Expand Up @@ -210,25 +220,43 @@ extension Pager {
DragGesture(minimumDistance: minimumDistance)
.onChanged({ value in
withAnimation {
self.draggingStartTime = self.draggingStartTime ?? value.time
let side = self.isHorizontal ? self.size.width : self.size.height
self.draggingOffset = value.translation.width * (self.pageDistance / side)
}
}).onEnded({ (value) in
let velocity = -Double(self.draggingOffset) / value.time.timeIntervalSince(self.draggingStartTime ?? Date())
var newPage = self.currentPage
if newPage == self.page, abs(velocity) > 1000 {
newPage = newPage + Int(velocity / abs(velocity))
}
let newOffset = value.translation.width * (self.pageDistance / side)

newPage = max(0, min(self.numberOfPages - 1, newPage))
let timeIncrement = value.time.timeIntervalSince(self.draggingTimestamp ?? value.time)
let offsetIncrement = (newOffset - self.draggingOffset) * (side / self.pageDistance)
if timeIncrement != 0 {
self.draggingVelocity = Double(offsetIncrement) / timeIncrement
}

withAnimation(.easeOut) {
self.draggingOffset = 0
self.pageIndex = newPage
self.draggingStartTime = nil
self.draggingTimestamp = value.time
self.draggingOffset = newOffset
}
})
.onEnded({ (value) in
self.onDragGestureEnded()
})
}

private func onDragGestureEnded() {
let velocity = -self.draggingVelocity
var newPage = self.currentPage
if newPage == self.page, abs(velocity) > 500 {
if isInifinitePager {
newPage = (newPage + Int(velocity / abs(velocity)) + self.numberOfPages) % self.numberOfPages
} else {
newPage = newPage + Int(velocity / abs(velocity))
}
}

newPage = max(0, min(self.numberOfPages - 1, newPage))

withAnimation(.easeOut) {
self.draggingOffset = 0
self.pageIndex = newPage
self.draggingVelocity = 0
self.draggingTimestamp = nil
}
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion SwiftUIPager.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SwiftUIPager"
s.version = "1.8.0-beta.1"
s.version = "1.8.0"
s.summary = "Native pager for SwiftUI. Easily to use, easy to customize."

s.description = <<-DESC
Expand Down
58 changes: 29 additions & 29 deletions SwiftUIPager.xcframework/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,106 +6,106 @@
<array>
<dict>
<key>LibraryIdentifier</key>
<string>ios-x86_64-maccatalyst</string>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>SwiftUIPager.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>x86_64</string>
<string>arm64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>maccatalyst</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>macos-x86_64</string>
<string>tvos-arm64</string>
<key>LibraryPath</key>
<string>SwiftUIPager.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>x86_64</string>
<string>arm64</string>
</array>
<key>SupportedPlatform</key>
<string>macos</string>
<string>tvos</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>tvos-arm64</string>
<string>ios-x86_64-simulator</string>
<key>LibraryPath</key>
<string>SwiftUIPager.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>tvos</string>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>tvos-x86_64-simulator</string>
<string>watchos-armv7k_arm64_32</string>
<key>LibraryPath</key>
<string>SwiftUIPager.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>x86_64</string>
<string>armv7k</string>
<string>arm64_32</string>
</array>
<key>SupportedPlatform</key>
<string>tvos</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
<string>watchos</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<string>ios-x86_64-maccatalyst</string>
<key>LibraryPath</key>
<string>SwiftUIPager.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>maccatalyst</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>watchos-i386-simulator</string>
<string>macos-x86_64</string>
<key>LibraryPath</key>
<string>SwiftUIPager.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>i386</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>watchos</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
<string>macos</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>watchos-armv7k_arm64_32</string>
<string>tvos-x86_64-simulator</string>
<key>LibraryPath</key>
<string>SwiftUIPager.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>armv7k</string>
<string>arm64_32</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>watchos</string>
<string>tvos</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-x86_64-simulator</string>
<string>watchos-i386-simulator</string>
<key>LibraryPath</key>
<string>SwiftUIPager.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>x86_64</string>
<string>i386</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<string>watchos</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
public enum GesturePriority {
case high
case simultaneous
case `default`
case normal
public static func == (a: SwiftUIPager.GesturePriority, b: SwiftUIPager.GesturePriority) -> Swift.Bool
public var hashValue: Swift.Int {
get
Expand All @@ -29,6 +29,7 @@ public enum SwipeInteractionArea {
}
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension Pager {
public func contentLoadingPolicy(_ value: SwiftUIPager.ContentLoadingPolicy) -> SwiftUIPager.Pager<Element, ID, PageView>
public func loopPages(_ value: Swift.Bool = true) -> SwiftUIPager.Pager<Element, ID, PageView>
public func disableDragging() -> SwiftUIPager.Pager<Element, ID, PageView>
public func allowsDragging(_ value: Swift.Bool = true) -> SwiftUIPager.Pager<Element, ID, PageView>
Expand All @@ -49,6 +50,11 @@ extension Pager {
public func padding(_ insets: SwiftUI.EdgeInsets) -> SwiftUIPager.Pager<Element, ID, PageView>
public func padding(_ edges: SwiftUI.Edge.Set = .all, _ length: CoreGraphics.CGFloat? = nil) -> SwiftUIPager.Pager<Element, ID, PageView>
}
public enum ContentLoadingPolicy : Swift.Equatable {
case lazy(recyclingRatio: Swift.UInt)
case eager
public static func == (a: SwiftUIPager.ContentLoadingPolicy, b: SwiftUIPager.ContentLoadingPolicy) -> Swift.Bool
}
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct Pager<Element, ID, PageView> : SwiftUI.View where Element : Swift.Equatable, ID : Swift.Hashable, PageView : SwiftUI.View {
public init(page: SwiftUI.Binding<Swift.Int>, data: [Element], id: Swift.KeyPath<Element, ID>, @SwiftUI.ViewBuilder content: @escaping (Element) -> PageView)
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
public enum GesturePriority {
case high
case simultaneous
case `default`
case normal
public static func == (a: SwiftUIPager.GesturePriority, b: SwiftUIPager.GesturePriority) -> Swift.Bool
public var hashValue: Swift.Int {
get
Expand All @@ -29,6 +29,7 @@ public enum SwipeInteractionArea {
}
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension Pager {
public func contentLoadingPolicy(_ value: SwiftUIPager.ContentLoadingPolicy) -> SwiftUIPager.Pager<Element, ID, PageView>
public func loopPages(_ value: Swift.Bool = true) -> SwiftUIPager.Pager<Element, ID, PageView>
public func disableDragging() -> SwiftUIPager.Pager<Element, ID, PageView>
public func allowsDragging(_ value: Swift.Bool = true) -> SwiftUIPager.Pager<Element, ID, PageView>
Expand All @@ -49,6 +50,11 @@ extension Pager {
public func padding(_ insets: SwiftUI.EdgeInsets) -> SwiftUIPager.Pager<Element, ID, PageView>
public func padding(_ edges: SwiftUI.Edge.Set = .all, _ length: CoreGraphics.CGFloat? = nil) -> SwiftUIPager.Pager<Element, ID, PageView>
}
public enum ContentLoadingPolicy : Swift.Equatable {
case lazy(recyclingRatio: Swift.UInt)
case eager
public static func == (a: SwiftUIPager.ContentLoadingPolicy, b: SwiftUIPager.ContentLoadingPolicy) -> Swift.Bool
}
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct Pager<Element, ID, PageView> : SwiftUI.View where Element : Swift.Equatable, ID : Swift.Hashable, PageView : SwiftUI.View {
public init(page: SwiftUI.Binding<Swift.Int>, data: [Element], id: Swift.KeyPath<Element, ID>, @SwiftUI.ViewBuilder content: @escaping (Element) -> PageView)
Expand Down
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit c2218ba

Please sign in to comment.