From da2180413e3146e36896e3d519db29e2f435a677 Mon Sep 17 00:00:00 2001 From: grant Date: Tue, 28 Apr 2020 15:01:38 +0800 Subject: [PATCH 001/212] support multi section collectionview --- Lib/CollectionViewPagingLayout.swift | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Lib/CollectionViewPagingLayout.swift b/Lib/CollectionViewPagingLayout.swift index b8dd88f..773c3fd 100644 --- a/Lib/CollectionViewPagingLayout.swift +++ b/Lib/CollectionViewPagingLayout.swift @@ -43,7 +43,13 @@ public class CollectionViewPagingLayout: UICollectionViewLayout { } private var numberOfItems: Int { - collectionView?.numberOfItems(inSection: 0) ?? 0 + var count = 0; + if let sectionCount = collectionView?.numberOfSections { + for i in (0 ..< sectionCount) { + count += collectionView?.numberOfItems(inSection: i) ?? 0 + } + } + return count } @@ -88,8 +94,23 @@ public class CollectionViewPagingLayout: UICollectionViewLayout { let endIndex = min(numberOfItems, initialEndIndex + startIndexOutOfBounds) var attributesArray: [UICollectionViewLayoutAttributes] = [] + let sectionCount = collectionView?.numberOfSections ?? 0 for index in startIndex.. Date: Mon, 4 May 2020 16:39:14 +0800 Subject: [PATCH 002/212] Update Lib/CollectionViewPagingLayout.swift Co-authored-by: Amir Khorsandi --- Lib/CollectionViewPagingLayout.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/CollectionViewPagingLayout.swift b/Lib/CollectionViewPagingLayout.swift index 773c3fd..04358e3 100644 --- a/Lib/CollectionViewPagingLayout.swift +++ b/Lib/CollectionViewPagingLayout.swift @@ -43,7 +43,7 @@ public class CollectionViewPagingLayout: UICollectionViewLayout { } private var numberOfItems: Int { - var count = 0; + var count = 0 if let sectionCount = collectionView?.numberOfSections { for i in (0 ..< sectionCount) { count += collectionView?.numberOfItems(inSection: i) ?? 0 From abc6604312d179e7ef8899bca4f07d4b69138208 Mon Sep 17 00:00:00 2001 From: grant Date: Mon, 4 May 2020 17:03:04 +0800 Subject: [PATCH 003/212] optimize layout calculation --- Lib/CollectionViewPagingLayout.swift | 36 +++++++++++----------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/Lib/CollectionViewPagingLayout.swift b/Lib/CollectionViewPagingLayout.swift index 773c3fd..8a56c60 100644 --- a/Lib/CollectionViewPagingLayout.swift +++ b/Lib/CollectionViewPagingLayout.swift @@ -43,13 +43,9 @@ public class CollectionViewPagingLayout: UICollectionViewLayout { } private var numberOfItems: Int { - var count = 0; - if let sectionCount = collectionView?.numberOfSections { - for i in (0 ..< sectionCount) { - count += collectionView?.numberOfItems(inSection: i) ?? 0 - } - } - return count + return (0..<(collectionView?.numberOfSections ?? 0)) + .compactMap { collectionView?.numberOfItems(inSection: $0) } + .reduce(0, +) } @@ -94,23 +90,19 @@ public class CollectionViewPagingLayout: UICollectionViewLayout { let endIndex = min(numberOfItems, initialEndIndex + startIndexOutOfBounds) var attributesArray: [UICollectionViewLayoutAttributes] = [] - let sectionCount = collectionView?.numberOfSections ?? 0 + var section = 0 + var numberOfItemsInSection = collectionView?.numberOfItems(inSection: section) ?? 0 + var numberOfItemsInPrevSections = 0 for index in startIndex..= numberOfItemsInSection { + numberOfItemsInPrevSections += numberOfItemsInSection + section += 1 + numberOfItemsInSection = collectionView?.numberOfItems(inSection: section) ?? 0 + item = index - numberOfItemsInPrevSections } - - let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(row: row, section: section)) + + let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: item, section: section)) let pageIndex = CGFloat(index) let progress = pageIndex - currentScrollOffset var zIndex = Int(-abs(round(progress))) From b12ef5ea384e01cd9b8eabdb1e6991be0982e4ad Mon Sep 17 00:00:00 2001 From: Amir Date: Mon, 4 May 2020 19:49:14 +0200 Subject: [PATCH 004/212] Cache numberOfItems --- Lib/CollectionViewPagingLayout.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/CollectionViewPagingLayout.swift b/Lib/CollectionViewPagingLayout.swift index 8a56c60..96d9dea 100644 --- a/Lib/CollectionViewPagingLayout.swift +++ b/Lib/CollectionViewPagingLayout.swift @@ -72,6 +72,7 @@ public class CollectionViewPagingLayout: UICollectionViewLayout { override public func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { let currentScrollOffset = self.currentScrollOffset + let numberOfItems = self.numberOfItems let attributesCount = numberOfVisibleItems ?? numberOfItems let visibleRangeMid = attributesCount / 2 let currentPageIndex = Int(round(currentScrollOffset)) From ea4f295aea7295bb1bfca33804536386cdc37a73 Mon Sep 17 00:00:00 2001 From: Amir Khorsandi Date: Mon, 4 May 2020 19:52:57 +0200 Subject: [PATCH 005/212] Bump version --- CollectionViewPagingLayout.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CollectionViewPagingLayout.podspec b/CollectionViewPagingLayout.podspec index d1980e7..06b66dc 100644 --- a/CollectionViewPagingLayout.podspec +++ b/CollectionViewPagingLayout.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "CollectionViewPagingLayout" - s.version = "0.1.3" + s.version = "0.1.4" s.summary = "Simple layout for making paging effects with UICollectionView." s.description = <<-DESC From 2b38e2539d43dfa1d3dca6be2decd32bc6bdced8 Mon Sep 17 00:00:00 2001 From: Amir Khorsandi Date: Tue, 5 May 2020 14:21:16 +0200 Subject: [PATCH 006/212] Remove extra return keyword --- Lib/CollectionViewPagingLayout.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/CollectionViewPagingLayout.swift b/Lib/CollectionViewPagingLayout.swift index 96d9dea..07311f1 100644 --- a/Lib/CollectionViewPagingLayout.swift +++ b/Lib/CollectionViewPagingLayout.swift @@ -43,7 +43,7 @@ public class CollectionViewPagingLayout: UICollectionViewLayout { } private var numberOfItems: Int { - return (0..<(collectionView?.numberOfSections ?? 0)) + (0..<(collectionView?.numberOfSections ?? 0)) .compactMap { collectionView?.numberOfItems(inSection: $0) } .reduce(0, +) } From af35f9f85baf338d0c2b4a65e33bd85bd837e07d Mon Sep 17 00:00:00 2001 From: Amir Khorsandi Date: Tue, 5 May 2020 15:04:53 +0200 Subject: [PATCH 007/212] Use UIWindow.size instead of UI Screen --- .../project.pbxproj | 7 +++ Samples/PagingLayoutSamples/AppDelegate.swift | 2 +- .../Modules/Main/MainViewController.xib | 46 +++++++++---------- .../BaseShapeCollectionViewCell.swift | 4 +- .../ScaleShapeCollectionViewCells.swift | 16 ++++--- .../Utilities/UIWindow+Size.swift | 16 +++++++ 6 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 Samples/PagingLayoutSamples/Utilities/UIWindow+Size.swift diff --git a/Samples/PagingLayoutSamples.xcodeproj/project.pbxproj b/Samples/PagingLayoutSamples.xcodeproj/project.pbxproj index 0395cc3..1bffd98 100644 --- a/Samples/PagingLayoutSamples.xcodeproj/project.pbxproj +++ b/Samples/PagingLayoutSamples.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 291224692400352F001B603A /* StackShapeCollectionViewCells.swift in Sources */ = {isa = PBXBuildFile; fileRef = 291224682400352F001B603A /* StackShapeCollectionViewCells.swift */; }; 2925CDDF23D4D21F00243F5F /* Card.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2925CDDE23D4D21F00243F5F /* Card.swift */; }; + 297AD305244C97FF002C4C1E /* UIWindow+Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = 297AD304244C97FF002C4C1E /* UIWindow+Size.swift */; }; 29B815B12414132100F1C824 /* SnapshotShapeCollectionViewCells.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B815B02414132100F1C824 /* SnapshotShapeCollectionViewCells.swift */; }; 29D9F94323F7F98800656A67 /* ShapesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D9F94223F7F98800656A67 /* ShapesViewController.swift */; }; 29D9F94523F7F99400656A67 /* ShapesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D9F94423F7F99400656A67 /* ShapesViewModel.swift */; }; @@ -64,6 +65,7 @@ /* Begin PBXFileReference section */ 291224682400352F001B603A /* StackShapeCollectionViewCells.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StackShapeCollectionViewCells.swift; sourceTree = ""; }; 2925CDDE23D4D21F00243F5F /* Card.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Card.swift; sourceTree = ""; }; + 297AD304244C97FF002C4C1E /* UIWindow+Size.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIWindow+Size.swift"; sourceTree = ""; }; 29B815B02414132100F1C824 /* SnapshotShapeCollectionViewCells.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnapshotShapeCollectionViewCells.swift; sourceTree = ""; }; 29D9F94223F7F98800656A67 /* ShapesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShapesViewController.swift; sourceTree = ""; }; 29D9F94423F7F99400656A67 /* ShapesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShapesViewModel.swift; sourceTree = ""; }; @@ -290,6 +292,7 @@ ABA0DA0D23F98ECD004A9C18 /* UICollectionViewCell+Utilities.swift */, AB500A5423B152500056BE37 /* ViewModelBased.swift */, 29D9F94F23F806C400656A67 /* Optional+Let.swift */, + 297AD304244C97FF002C4C1E /* UIWindow+Size.swift */, ); path = Utilities; sourceTree = ""; @@ -535,6 +538,7 @@ 291224692400352F001B603A /* StackShapeCollectionViewCells.swift in Sources */, ABA0DA0823F98B65004A9C18 /* ShapeCardView.swift in Sources */, ABC242CC23B6831400DBD4D6 /* Photo.swift in Sources */, + 297AD305244C97FF002C4C1E /* UIWindow+Size.swift in Sources */, 29D9F94323F7F98800656A67 /* ShapesViewController.swift in Sources */, AB500A5823B154210056BE37 /* Fruit.swift in Sources */, 29D9F94523F7F99400656A67 /* ShapesViewModel.swift in Sources */, @@ -689,6 +693,7 @@ ); PRODUCT_BUNDLE_IDENTIFIER = app.amir.paginglayout; PRODUCT_NAME = "Paging Layout"; + SUPPORTS_MACCATALYST = NO; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = 1; }; @@ -699,6 +704,7 @@ baseConfigurationReference = 444EC9E8B3BA262F3697984F /* Pods-PagingLayoutSamples.release.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = "PagingLayoutSamples/Paging Layout.entitlements"; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 4J5W7CJ2ZV; INFOPLIST_FILE = PagingLayoutSamples/Info.plist; @@ -708,6 +714,7 @@ ); PRODUCT_BUNDLE_IDENTIFIER = app.amir.paginglayout; PRODUCT_NAME = "Paging Layout"; + SUPPORTS_MACCATALYST = NO; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = 1; }; diff --git a/Samples/PagingLayoutSamples/AppDelegate.swift b/Samples/PagingLayoutSamples/AppDelegate.swift index 0c8b6a5..9aaf916 100644 --- a/Samples/PagingLayoutSamples/AppDelegate.swift +++ b/Samples/PagingLayoutSamples/AppDelegate.swift @@ -18,7 +18,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. - window = UIWindow(frame: UIScreen.main.bounds) + window = UIWindow() navigationController = UINavigationController() navigationController.isNavigationBarHidden = true navigationController.setViewControllers([MainViewController.instantiate()], animated: false) diff --git a/Samples/PagingLayoutSamples/Modules/Main/MainViewController.xib b/Samples/PagingLayoutSamples/Modules/Main/MainViewController.xib index 3292e12..60074f2 100644 --- a/Samples/PagingLayoutSamples/Modules/Main/MainViewController.xib +++ b/Samples/PagingLayoutSamples/Modules/Main/MainViewController.xib @@ -1,8 +1,8 @@ - + - + @@ -18,17 +18,17 @@ - +