├── .gitignore
├── LICENSE
├── README.md
├── UICollectionViewHorizontalPaging.xcodeproj
├── project.pbxproj
└── project.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcshareddata
│ └── IDEWorkspaceChecks.plist
├── UICollectionViewHorizontalPaging
├── AppDelegate.swift
├── Assets.xcassets
│ └── AppIcon.appiconset
│ │ └── Contents.json
├── Base.lproj
│ ├── LaunchScreen.storyboard
│ └── Main.storyboard
├── CenterViewFlowLayout.swift
├── Info.plist
├── UIColorExtensions.swift
└── ViewController.swift
└── img
├── 1.gif
├── 2.png
└── 3.png
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4 |
5 | ## Build generated
6 | build/
7 | DerivedData
8 |
9 | ## Various settings
10 | *.pbxuser
11 | !default.pbxuser
12 | *.mode1v3
13 | !default.mode1v3
14 | *.mode2v3
15 | !default.mode2v3
16 | *.perspectivev3
17 | !default.perspectivev3
18 | xcuserdata
19 |
20 | ## Other
21 | *.xccheckout
22 | *.moved-aside
23 | *.xcuserstate
24 | *.xcscmblueprint
25 |
26 | ## Obj-C/Swift specific
27 | *.hmap
28 | *.ipa
29 |
30 | ## Playgrounds
31 | timeline.xctimeline
32 | playground.xcworkspace
33 |
34 | # Swift Package Manager
35 | #
36 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
37 | # Packages/
38 | .build/
39 |
40 | # CocoaPods
41 | #
42 | # We recommend against adding the Pods directory to your .gitignore. However
43 | # you should judge for yourself, the pros and cons are mentioned at:
44 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
45 | #
46 | # Pods/
47 |
48 | # Carthage
49 | #
50 | # Add this line if you want to avoid checking in source code from Carthage dependencies.
51 | # Carthage/Checkouts
52 |
53 | Carthage/Build
54 |
55 | # fastlane
56 | #
57 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
58 | # screenshots whenever they are needed.
59 | # For more information about the recommended setup visit:
60 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md
61 |
62 | fastlane/report.xml
63 | fastlane/screenshots
64 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Maxim Bilan
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # iOS UICollectionView Page Scrolling
2 |
3 | I would like to provide an example how to do a horizontal page scrolling using UICollectionView.
4 |
5 | 
6 |
7 | Please set up isPaggingEnabled property to YES for the collection view object. Or via Interface Builder:
8 |
9 | 
10 |
11 | And let’s create a custom class for the flow layout:
12 |
13 | 
14 |
15 | Please use the following code:
16 |
17 |
18 | import UIKit
19 |
20 | class CenterViewFlowLayout: UICollectionViewFlowLayout {
21 |
22 | override func collectionViewContentSize() -> CGSize {
23 | // Only support single section for now.
24 | // Only support Horizontal scroll
25 | let count = self.collectionView?.dataSource?.collectionView(self.collectionView!, numberOfItemsInSection: 0)
26 | let canvasSize = self.collectionView!.frame.size
27 | var contentSize = canvasSize
28 | if self.scrollDirection == UICollectionViewScrollDirection.Horizontal {
29 | let rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1)
30 | let columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1)
31 | let page = ceilf(Float(count!) / Float(rowCount * columnCount))
32 | contentSize.width = CGFloat(page) * canvasSize.width
33 | }
34 | return contentSize
35 | }
36 |
37 | func frameForItemAtIndexPath(indexPath: NSIndexPath) -> CGRect {
38 | let canvasSize = self.collectionView!.frame.size
39 | let rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1)
40 | let columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1)
41 |
42 | let pageMarginX = (canvasSize.width - CGFloat(columnCount) * self.itemSize.width - (columnCount > 1 ? CGFloat(columnCount - 1) * self.minimumLineSpacing : 0)) / 2.0
43 | let pageMarginY = (canvasSize.height - CGFloat(rowCount) * self.itemSize.height - (rowCount > 1 ? CGFloat(rowCount - 1) * self.minimumInteritemSpacing : 0)) / 2.0
44 |
45 | let page = Int(CGFloat(indexPath.row) / CGFloat(rowCount * columnCount))
46 | let remainder = Int(CGFloat(indexPath.row) - CGFloat(page) * CGFloat(rowCount * columnCount))
47 | let row = Int(CGFloat(remainder) / CGFloat(columnCount))
48 | let column = Int(CGFloat(remainder) - CGFloat(row) * CGFloat(columnCount))
49 |
50 | var cellFrame = CGRectZero
51 | cellFrame.origin.x = pageMarginX + CGFloat(column) * (self.itemSize.width + self.minimumLineSpacing)
52 | cellFrame.origin.y = pageMarginY + CGFloat(row) * (self.itemSize.height + self.minimumInteritemSpacing)
53 | cellFrame.size.width = self.itemSize.width
54 | cellFrame.size.height = self.itemSize.height
55 |
56 | if self.scrollDirection == UICollectionViewScrollDirection.Horizontal {
57 | cellFrame.origin.x += CGFloat(page) * canvasSize.width
58 | }
59 |
60 | return cellFrame
61 | }
62 |
63 | override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
64 | let attr = super.layoutAttributesForItemAtIndexPath(indexPath)?.copy() as! UICollectionViewLayoutAttributes?
65 | attr!.frame = self.frameForItemAtIndexPath(indexPath)
66 | return attr
67 | }
68 |
69 | override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
70 | let originAttrs = super.layoutAttributesForElementsInRect(rect)
71 | var attrs: [UICollectionViewLayoutAttributes]? = Array()
72 |
73 | for attr in originAttrs! {
74 | let idxPath = attr.indexPath
75 | let itemFrame = self.frameForItemAtIndexPath(idxPath)
76 | if CGRectIntersectsRect(itemFrame, rect) {
77 | let nAttr = self.layoutAttributesForItemAtIndexPath(idxPath)
78 | attrs?.append(nAttr!)
79 | }
80 | }
81 |
82 | return attrs
83 | }
84 |
85 | }
86 |
87 |
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 46;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | 6DE94FFE1C64957000B90C9F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DE94FFD1C64957000B90C9F /* AppDelegate.swift */; };
11 | 6DE950001C64957000B90C9F /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DE94FFF1C64957000B90C9F /* ViewController.swift */; };
12 | 6DE950031C64957000B90C9F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6DE950011C64957000B90C9F /* Main.storyboard */; };
13 | 6DE950051C64957000B90C9F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6DE950041C64957000B90C9F /* Assets.xcassets */; };
14 | 6DE950081C64957000B90C9F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6DE950061C64957000B90C9F /* LaunchScreen.storyboard */; };
15 | 943B124E1C666506009E3AAD /* UIColorExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 943B124D1C666506009E3AAD /* UIColorExtensions.swift */; };
16 | 943B12501C6668B2009E3AAD /* CenterViewFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 943B124F1C6668B2009E3AAD /* CenterViewFlowLayout.swift */; };
17 | /* End PBXBuildFile section */
18 |
19 | /* Begin PBXFileReference section */
20 | 6DE94FFA1C64957000B90C9F /* UICollectionViewHorizontalPaging.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UICollectionViewHorizontalPaging.app; sourceTree = BUILT_PRODUCTS_DIR; };
21 | 6DE94FFD1C64957000B90C9F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
22 | 6DE94FFF1C64957000B90C9F /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; };
23 | 6DE950021C64957000B90C9F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
24 | 6DE950041C64957000B90C9F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
25 | 6DE950071C64957000B90C9F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; };
26 | 6DE950091C64957000B90C9F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
27 | 943B124D1C666506009E3AAD /* UIColorExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIColorExtensions.swift; sourceTree = ""; };
28 | 943B124F1C6668B2009E3AAD /* CenterViewFlowLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CenterViewFlowLayout.swift; sourceTree = ""; };
29 | /* End PBXFileReference section */
30 |
31 | /* Begin PBXFrameworksBuildPhase section */
32 | 6DE94FF71C64957000B90C9F /* Frameworks */ = {
33 | isa = PBXFrameworksBuildPhase;
34 | buildActionMask = 2147483647;
35 | files = (
36 | );
37 | runOnlyForDeploymentPostprocessing = 0;
38 | };
39 | /* End PBXFrameworksBuildPhase section */
40 |
41 | /* Begin PBXGroup section */
42 | 6D8F0B091C6896D7006C87E0 /* Sources */ = {
43 | isa = PBXGroup;
44 | children = (
45 | 6DE94FFD1C64957000B90C9F /* AppDelegate.swift */,
46 | 6DE94FFF1C64957000B90C9F /* ViewController.swift */,
47 | 943B124F1C6668B2009E3AAD /* CenterViewFlowLayout.swift */,
48 | 943B124D1C666506009E3AAD /* UIColorExtensions.swift */,
49 | );
50 | name = Sources;
51 | sourceTree = "";
52 | };
53 | 6D8F0B0A1C6896DD006C87E0 /* UI */ = {
54 | isa = PBXGroup;
55 | children = (
56 | 6DE950011C64957000B90C9F /* Main.storyboard */,
57 | 6DE950061C64957000B90C9F /* LaunchScreen.storyboard */,
58 | );
59 | name = UI;
60 | sourceTree = "";
61 | };
62 | 6D8F0B0B1C6896E2006C87E0 /* Assets */ = {
63 | isa = PBXGroup;
64 | children = (
65 | 6DE950041C64957000B90C9F /* Assets.xcassets */,
66 | );
67 | name = Assets;
68 | sourceTree = "";
69 | };
70 | 6D8F0B0C1C6896E6006C87E0 /* Supporting Files */ = {
71 | isa = PBXGroup;
72 | children = (
73 | 6DE950091C64957000B90C9F /* Info.plist */,
74 | );
75 | name = "Supporting Files";
76 | sourceTree = "";
77 | };
78 | 6DE94FF11C64957000B90C9F = {
79 | isa = PBXGroup;
80 | children = (
81 | 6DE94FFC1C64957000B90C9F /* UICollectionViewHorizontalPaging */,
82 | 6DE94FFB1C64957000B90C9F /* Products */,
83 | );
84 | sourceTree = "";
85 | };
86 | 6DE94FFB1C64957000B90C9F /* Products */ = {
87 | isa = PBXGroup;
88 | children = (
89 | 6DE94FFA1C64957000B90C9F /* UICollectionViewHorizontalPaging.app */,
90 | );
91 | name = Products;
92 | sourceTree = "";
93 | };
94 | 6DE94FFC1C64957000B90C9F /* UICollectionViewHorizontalPaging */ = {
95 | isa = PBXGroup;
96 | children = (
97 | 6D8F0B091C6896D7006C87E0 /* Sources */,
98 | 6D8F0B0A1C6896DD006C87E0 /* UI */,
99 | 6D8F0B0B1C6896E2006C87E0 /* Assets */,
100 | 6D8F0B0C1C6896E6006C87E0 /* Supporting Files */,
101 | );
102 | path = UICollectionViewHorizontalPaging;
103 | sourceTree = "";
104 | };
105 | /* End PBXGroup section */
106 |
107 | /* Begin PBXNativeTarget section */
108 | 6DE94FF91C64957000B90C9F /* UICollectionViewHorizontalPaging */ = {
109 | isa = PBXNativeTarget;
110 | buildConfigurationList = 6DE9500C1C64957000B90C9F /* Build configuration list for PBXNativeTarget "UICollectionViewHorizontalPaging" */;
111 | buildPhases = (
112 | 6DE94FF61C64957000B90C9F /* Sources */,
113 | 6DE94FF71C64957000B90C9F /* Frameworks */,
114 | 6DE94FF81C64957000B90C9F /* Resources */,
115 | );
116 | buildRules = (
117 | );
118 | dependencies = (
119 | );
120 | name = UICollectionViewHorizontalPaging;
121 | productName = UICollectionViewHorizontalPaging;
122 | productReference = 6DE94FFA1C64957000B90C9F /* UICollectionViewHorizontalPaging.app */;
123 | productType = "com.apple.product-type.application";
124 | };
125 | /* End PBXNativeTarget section */
126 |
127 | /* Begin PBXProject section */
128 | 6DE94FF21C64957000B90C9F /* Project object */ = {
129 | isa = PBXProject;
130 | attributes = {
131 | LastSwiftUpdateCheck = 0720;
132 | LastUpgradeCheck = 1000;
133 | ORGANIZATIONNAME = "Maxim Bilan";
134 | TargetAttributes = {
135 | 6DE94FF91C64957000B90C9F = {
136 | CreatedOnToolsVersion = 7.2.1;
137 | LastSwiftMigration = 1000;
138 | };
139 | };
140 | };
141 | buildConfigurationList = 6DE94FF51C64957000B90C9F /* Build configuration list for PBXProject "UICollectionViewHorizontalPaging" */;
142 | compatibilityVersion = "Xcode 3.2";
143 | developmentRegion = English;
144 | hasScannedForEncodings = 0;
145 | knownRegions = (
146 | en,
147 | Base,
148 | );
149 | mainGroup = 6DE94FF11C64957000B90C9F;
150 | productRefGroup = 6DE94FFB1C64957000B90C9F /* Products */;
151 | projectDirPath = "";
152 | projectRoot = "";
153 | targets = (
154 | 6DE94FF91C64957000B90C9F /* UICollectionViewHorizontalPaging */,
155 | );
156 | };
157 | /* End PBXProject section */
158 |
159 | /* Begin PBXResourcesBuildPhase section */
160 | 6DE94FF81C64957000B90C9F /* Resources */ = {
161 | isa = PBXResourcesBuildPhase;
162 | buildActionMask = 2147483647;
163 | files = (
164 | 6DE950081C64957000B90C9F /* LaunchScreen.storyboard in Resources */,
165 | 6DE950051C64957000B90C9F /* Assets.xcassets in Resources */,
166 | 6DE950031C64957000B90C9F /* Main.storyboard in Resources */,
167 | );
168 | runOnlyForDeploymentPostprocessing = 0;
169 | };
170 | /* End PBXResourcesBuildPhase section */
171 |
172 | /* Begin PBXSourcesBuildPhase section */
173 | 6DE94FF61C64957000B90C9F /* Sources */ = {
174 | isa = PBXSourcesBuildPhase;
175 | buildActionMask = 2147483647;
176 | files = (
177 | 943B124E1C666506009E3AAD /* UIColorExtensions.swift in Sources */,
178 | 6DE950001C64957000B90C9F /* ViewController.swift in Sources */,
179 | 6DE94FFE1C64957000B90C9F /* AppDelegate.swift in Sources */,
180 | 943B12501C6668B2009E3AAD /* CenterViewFlowLayout.swift in Sources */,
181 | );
182 | runOnlyForDeploymentPostprocessing = 0;
183 | };
184 | /* End PBXSourcesBuildPhase section */
185 |
186 | /* Begin PBXVariantGroup section */
187 | 6DE950011C64957000B90C9F /* Main.storyboard */ = {
188 | isa = PBXVariantGroup;
189 | children = (
190 | 6DE950021C64957000B90C9F /* Base */,
191 | );
192 | name = Main.storyboard;
193 | sourceTree = "";
194 | };
195 | 6DE950061C64957000B90C9F /* LaunchScreen.storyboard */ = {
196 | isa = PBXVariantGroup;
197 | children = (
198 | 6DE950071C64957000B90C9F /* Base */,
199 | );
200 | name = LaunchScreen.storyboard;
201 | sourceTree = "";
202 | };
203 | /* End PBXVariantGroup section */
204 |
205 | /* Begin XCBuildConfiguration section */
206 | 6DE9500A1C64957000B90C9F /* Debug */ = {
207 | isa = XCBuildConfiguration;
208 | buildSettings = {
209 | ALWAYS_SEARCH_USER_PATHS = NO;
210 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
211 | CLANG_CXX_LIBRARY = "libc++";
212 | CLANG_ENABLE_MODULES = YES;
213 | CLANG_ENABLE_OBJC_ARC = YES;
214 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
215 | CLANG_WARN_BOOL_CONVERSION = YES;
216 | CLANG_WARN_COMMA = YES;
217 | CLANG_WARN_CONSTANT_CONVERSION = YES;
218 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
219 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
220 | CLANG_WARN_EMPTY_BODY = YES;
221 | CLANG_WARN_ENUM_CONVERSION = YES;
222 | CLANG_WARN_INFINITE_RECURSION = YES;
223 | CLANG_WARN_INT_CONVERSION = YES;
224 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
225 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
226 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
227 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
228 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
229 | CLANG_WARN_STRICT_PROTOTYPES = YES;
230 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
231 | CLANG_WARN_UNREACHABLE_CODE = YES;
232 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
233 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
234 | COPY_PHASE_STRIP = NO;
235 | DEBUG_INFORMATION_FORMAT = dwarf;
236 | ENABLE_STRICT_OBJC_MSGSEND = YES;
237 | ENABLE_TESTABILITY = YES;
238 | GCC_C_LANGUAGE_STANDARD = gnu99;
239 | GCC_DYNAMIC_NO_PIC = NO;
240 | GCC_NO_COMMON_BLOCKS = YES;
241 | GCC_OPTIMIZATION_LEVEL = 0;
242 | GCC_PREPROCESSOR_DEFINITIONS = (
243 | "DEBUG=1",
244 | "$(inherited)",
245 | );
246 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
247 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
248 | GCC_WARN_UNDECLARED_SELECTOR = YES;
249 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
250 | GCC_WARN_UNUSED_FUNCTION = YES;
251 | GCC_WARN_UNUSED_VARIABLE = YES;
252 | IPHONEOS_DEPLOYMENT_TARGET = 9.2;
253 | MTL_ENABLE_DEBUG_INFO = YES;
254 | ONLY_ACTIVE_ARCH = YES;
255 | SDKROOT = iphoneos;
256 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
257 | TARGETED_DEVICE_FAMILY = "1,2";
258 | };
259 | name = Debug;
260 | };
261 | 6DE9500B1C64957000B90C9F /* Release */ = {
262 | isa = XCBuildConfiguration;
263 | buildSettings = {
264 | ALWAYS_SEARCH_USER_PATHS = NO;
265 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
266 | CLANG_CXX_LIBRARY = "libc++";
267 | CLANG_ENABLE_MODULES = YES;
268 | CLANG_ENABLE_OBJC_ARC = YES;
269 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
270 | CLANG_WARN_BOOL_CONVERSION = YES;
271 | CLANG_WARN_COMMA = YES;
272 | CLANG_WARN_CONSTANT_CONVERSION = YES;
273 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
274 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
275 | CLANG_WARN_EMPTY_BODY = YES;
276 | CLANG_WARN_ENUM_CONVERSION = YES;
277 | CLANG_WARN_INFINITE_RECURSION = YES;
278 | CLANG_WARN_INT_CONVERSION = YES;
279 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
280 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
281 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
282 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
283 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
284 | CLANG_WARN_STRICT_PROTOTYPES = YES;
285 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
286 | CLANG_WARN_UNREACHABLE_CODE = YES;
287 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
288 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
289 | COPY_PHASE_STRIP = NO;
290 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
291 | ENABLE_NS_ASSERTIONS = NO;
292 | ENABLE_STRICT_OBJC_MSGSEND = YES;
293 | GCC_C_LANGUAGE_STANDARD = gnu99;
294 | GCC_NO_COMMON_BLOCKS = YES;
295 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
296 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
297 | GCC_WARN_UNDECLARED_SELECTOR = YES;
298 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
299 | GCC_WARN_UNUSED_FUNCTION = YES;
300 | GCC_WARN_UNUSED_VARIABLE = YES;
301 | IPHONEOS_DEPLOYMENT_TARGET = 9.2;
302 | MTL_ENABLE_DEBUG_INFO = NO;
303 | SDKROOT = iphoneos;
304 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
305 | TARGETED_DEVICE_FAMILY = "1,2";
306 | VALIDATE_PRODUCT = YES;
307 | };
308 | name = Release;
309 | };
310 | 6DE9500D1C64957000B90C9F /* Debug */ = {
311 | isa = XCBuildConfiguration;
312 | buildSettings = {
313 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
314 | INFOPLIST_FILE = UICollectionViewHorizontalPaging/Info.plist;
315 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
316 | PRODUCT_BUNDLE_IDENTIFIER = maximbilan.UICollectionViewHorizontalPaging;
317 | PRODUCT_NAME = "$(TARGET_NAME)";
318 | SWIFT_VERSION = 4.2;
319 | };
320 | name = Debug;
321 | };
322 | 6DE9500E1C64957000B90C9F /* Release */ = {
323 | isa = XCBuildConfiguration;
324 | buildSettings = {
325 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
326 | INFOPLIST_FILE = UICollectionViewHorizontalPaging/Info.plist;
327 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
328 | PRODUCT_BUNDLE_IDENTIFIER = maximbilan.UICollectionViewHorizontalPaging;
329 | PRODUCT_NAME = "$(TARGET_NAME)";
330 | SWIFT_VERSION = 4.2;
331 | };
332 | name = Release;
333 | };
334 | /* End XCBuildConfiguration section */
335 |
336 | /* Begin XCConfigurationList section */
337 | 6DE94FF51C64957000B90C9F /* Build configuration list for PBXProject "UICollectionViewHorizontalPaging" */ = {
338 | isa = XCConfigurationList;
339 | buildConfigurations = (
340 | 6DE9500A1C64957000B90C9F /* Debug */,
341 | 6DE9500B1C64957000B90C9F /* Release */,
342 | );
343 | defaultConfigurationIsVisible = 0;
344 | defaultConfigurationName = Release;
345 | };
346 | 6DE9500C1C64957000B90C9F /* Build configuration list for PBXNativeTarget "UICollectionViewHorizontalPaging" */ = {
347 | isa = XCConfigurationList;
348 | buildConfigurations = (
349 | 6DE9500D1C64957000B90C9F /* Debug */,
350 | 6DE9500E1C64957000B90C9F /* Release */,
351 | );
352 | defaultConfigurationIsVisible = 0;
353 | defaultConfigurationName = Release;
354 | };
355 | /* End XCConfigurationList section */
356 | };
357 | rootObject = 6DE94FF21C64957000B90C9F /* Project object */;
358 | }
359 |
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // UICollectionViewHorizontalPaging
4 | //
5 | // Created by Maxim Bilan on 2/5/16.
6 | // Copyright © 2016 Maxim Bilan. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
18 | // Override point for customization after application launch.
19 | return true
20 | }
21 |
22 | func applicationWillResignActive(_ application: UIApplication) {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
25 | }
26 |
27 | func applicationDidEnterBackground(_ application: UIApplication) {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | func applicationWillEnterForeground(_ application: UIApplication) {
33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
34 | }
35 |
36 | func applicationDidBecomeActive(_ application: UIApplication) {
37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38 | }
39 |
40 | func applicationWillTerminate(_ application: UIApplication) {
41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42 | }
43 |
44 |
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "29x29",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "29x29",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "40x40",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "40x40",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "60x60",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "60x60",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "ipad",
35 | "size" : "29x29",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "ipad",
40 | "size" : "29x29",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "40x40",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "40x40",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "76x76",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "76x76",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "83.5x83.5",
66 | "scale" : "2x"
67 | }
68 | ],
69 | "info" : {
70 | "version" : 1,
71 | "author" : "xcode"
72 | }
73 | }
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging/CenterViewFlowLayout.swift:
--------------------------------------------------------------------------------
1 | //
2 | // CenterViewFlowLayout.swift
3 | // UICollectionViewHorizontalPaging
4 | //
5 | // Created by Maxim on 2/6/16.
6 | // Copyright © 2016 Maxim Bilan. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class CenterViewFlowLayout: UICollectionViewFlowLayout {
12 |
13 | override var collectionViewContentSize : CGSize {
14 | // Only support single section for now.
15 | // Only support Horizontal scroll
16 | let count = self.collectionView?.dataSource?.collectionView(self.collectionView!, numberOfItemsInSection: 0)
17 | let canvasSize = self.collectionView!.frame.size
18 | var contentSize = canvasSize
19 | if self.scrollDirection == UICollectionView.ScrollDirection.horizontal {
20 | let rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1)
21 | let columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1)
22 | let page = ceilf(Float(count!) / Float(rowCount * columnCount))
23 | contentSize.width = CGFloat(page) * canvasSize.width
24 | }
25 | return contentSize
26 | }
27 |
28 | func frameForItemAtIndexPath(_ indexPath: IndexPath) -> CGRect {
29 | let canvasSize = self.collectionView!.frame.size
30 | let rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1)
31 | let columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1)
32 |
33 | let pageMarginX = (canvasSize.width - CGFloat(columnCount) * self.itemSize.width - (columnCount > 1 ? CGFloat(columnCount - 1) * self.minimumLineSpacing : 0)) / 2.0
34 | let pageMarginY = (canvasSize.height - CGFloat(rowCount) * self.itemSize.height - (rowCount > 1 ? CGFloat(rowCount - 1) * self.minimumInteritemSpacing : 0)) / 2.0
35 |
36 | let page = Int(CGFloat(indexPath.row) / CGFloat(rowCount * columnCount))
37 | let remainder = Int(CGFloat(indexPath.row) - CGFloat(page) * CGFloat(rowCount * columnCount))
38 | let row = Int(CGFloat(remainder) / CGFloat(columnCount))
39 | let column = Int(CGFloat(remainder) - CGFloat(row) * CGFloat(columnCount))
40 |
41 | var cellFrame = CGRect.zero
42 | cellFrame.origin.x = pageMarginX + CGFloat(column) * (self.itemSize.width + self.minimumLineSpacing)
43 | cellFrame.origin.y = pageMarginY + CGFloat(row) * (self.itemSize.height + self.minimumInteritemSpacing)
44 | cellFrame.size.width = self.itemSize.width
45 | cellFrame.size.height = self.itemSize.height
46 |
47 | if self.scrollDirection == UICollectionView.ScrollDirection.horizontal {
48 | cellFrame.origin.x += CGFloat(page) * canvasSize.width
49 | }
50 |
51 | return cellFrame
52 | }
53 |
54 | override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
55 | let attr = super.layoutAttributesForItem(at: indexPath)?.copy() as! UICollectionViewLayoutAttributes?
56 | attr!.frame = self.frameForItemAtIndexPath(indexPath)
57 | return attr
58 | }
59 |
60 | override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
61 | let originAttrs = super.layoutAttributesForElements(in: rect)
62 | var attrs: [UICollectionViewLayoutAttributes]? = Array()
63 |
64 | for attr in originAttrs! {
65 | let idxPath = attr.indexPath
66 | let itemFrame = self.frameForItemAtIndexPath(idxPath)
67 | if itemFrame.intersects(rect) {
68 | let nAttr = self.layoutAttributesForItem(at: idxPath)
69 | attrs?.append(nAttr!)
70 | }
71 | }
72 |
73 | return attrs
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | 1
23 | LSRequiresIPhoneOS
24 |
25 | UILaunchStoryboardName
26 | LaunchScreen
27 | UIMainStoryboardFile
28 | Main
29 | UIRequiredDeviceCapabilities
30 |
31 | armv7
32 |
33 | UISupportedInterfaceOrientations
34 |
35 | UIInterfaceOrientationPortrait
36 | UIInterfaceOrientationLandscapeLeft
37 | UIInterfaceOrientationLandscapeRight
38 |
39 | UISupportedInterfaceOrientations~ipad
40 |
41 | UIInterfaceOrientationPortrait
42 | UIInterfaceOrientationPortraitUpsideDown
43 | UIInterfaceOrientationLandscapeLeft
44 | UIInterfaceOrientationLandscapeRight
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging/UIColorExtensions.swift:
--------------------------------------------------------------------------------
1 | //
2 | // UIColorExtensions.swift
3 | // UICollectionViewHorizontalPaging
4 | //
5 | // Created by Maxim on 2/6/16.
6 | // Copyright © 2016 Maxim Bilan. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | extension UIColor {
12 | class func randomColor() -> UIColor {
13 |
14 | let hue = CGFloat(arc4random() % 100) / 100
15 | let saturation = CGFloat(arc4random() % 100) / 100
16 | let brightness = CGFloat(arc4random() % 100) / 100
17 |
18 | return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0)
19 | }
20 | }
--------------------------------------------------------------------------------
/UICollectionViewHorizontalPaging/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // UICollectionViewHorizontalPaging
4 | //
5 | // Created by Maxim Bilan on 2/5/16.
6 | // Copyright © 2016 Maxim Bilan. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class CollectionViewCell : UICollectionViewCell {
12 | @IBOutlet weak var imageView: UIImageView!
13 | }
14 |
15 | class ViewController: UIViewController, UICollectionViewDataSource {
16 |
17 | let reuseIdentifier = "collectionViewCellId"
18 |
19 | @IBOutlet weak var collectionView: UICollectionView!
20 |
21 | func numberOfSections(in collectionView: UICollectionView) -> Int {
22 | return 1
23 | }
24 |
25 | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
26 | return 20
27 | }
28 |
29 | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
30 | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
31 |
32 | cell.imageView.backgroundColor = UIColor.randomColor()
33 |
34 | return cell
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/img/1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maximbilan/UICollectionViewHorizontalPaging/7c8ec956cb8c66b1d0af6ae8072514d0ef9cd195/img/1.gif
--------------------------------------------------------------------------------
/img/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maximbilan/UICollectionViewHorizontalPaging/7c8ec956cb8c66b1d0af6ae8072514d0ef9cd195/img/2.png
--------------------------------------------------------------------------------
/img/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maximbilan/UICollectionViewHorizontalPaging/7c8ec956cb8c66b1d0af6ae8072514d0ef9cd195/img/3.png
--------------------------------------------------------------------------------