├── .gitignore ├── Example ├── Example.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── Example │ ├── AppDelegate.swift │ ├── Resources │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── Contents.json │ ├── Base.lproj │ │ └── LaunchScreen.storyboard │ └── Info.plist │ └── ViewController.swift ├── LICENSE ├── Media ├── example-multiple-items-per-page.gif ├── example-skip.gif ├── example-vertical.gif ├── example.gif ├── spacing-vertical.jpg └── spacing.png ├── PagingCollectionViewLayout.podspec ├── PagingCollectionViewLayout.swift └── README.md /.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 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | *.DS_Store 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | *.dSYM.zip 30 | *.dSYM 31 | 32 | ## Playgrounds 33 | timeline.xctimeline 34 | playground.xcworkspace 35 | 36 | # Swift Package Manager 37 | # 38 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 39 | # Packages/ 40 | # Package.pins 41 | # Package.resolved 42 | .build/ 43 | 44 | # CocoaPods 45 | # 46 | # We recommend against adding the Pods directory to your .gitignore. However 47 | # you should judge for yourself, the pros and cons are mentioned at: 48 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 49 | # 50 | # Pods/ 51 | 52 | # Carthage 53 | # 54 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 55 | # Carthage/Checkouts 56 | 57 | Carthage/Build 58 | 59 | # fastlane 60 | # 61 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 62 | # screenshots whenever they are needed. 63 | # For more information about the recommended setup visit: 64 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 65 | 66 | fastlane/report.xml 67 | fastlane/Preview.html 68 | fastlane/screenshots/**/*.png 69 | fastlane/test_output 70 | 71 | -------------------------------------------------------------------------------- /Example/Example.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | A2EB7729230075F700FD0C79 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2EB7728230075F700FD0C79 /* AppDelegate.swift */; }; 11 | A2EB772B230075F700FD0C79 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2EB772A230075F700FD0C79 /* ViewController.swift */; }; 12 | A2EB7730230075F800FD0C79 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A2EB772F230075F800FD0C79 /* Assets.xcassets */; }; 13 | A2EB7733230075F800FD0C79 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A2EB7731230075F800FD0C79 /* LaunchScreen.storyboard */; }; 14 | A2EB77422300809100FD0C79 /* PagingCollectionViewLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2EB77412300809100FD0C79 /* PagingCollectionViewLayout.swift */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXFileReference section */ 18 | A2EB7725230075F700FD0C79 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; }; 19 | A2EB7728230075F700FD0C79 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 20 | A2EB772A230075F700FD0C79 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 21 | A2EB772F230075F800FD0C79 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 22 | A2EB7732230075F800FD0C79 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 23 | A2EB7734230075F800FD0C79 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 24 | A2EB77412300809100FD0C79 /* PagingCollectionViewLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = PagingCollectionViewLayout.swift; path = ../../PagingCollectionViewLayout.swift; sourceTree = ""; }; 25 | /* End PBXFileReference section */ 26 | 27 | /* Begin PBXFrameworksBuildPhase section */ 28 | A2EB7722230075F700FD0C79 /* Frameworks */ = { 29 | isa = PBXFrameworksBuildPhase; 30 | buildActionMask = 2147483647; 31 | files = ( 32 | ); 33 | runOnlyForDeploymentPostprocessing = 0; 34 | }; 35 | /* End PBXFrameworksBuildPhase section */ 36 | 37 | /* Begin PBXGroup section */ 38 | A2EB771C230075F700FD0C79 = { 39 | isa = PBXGroup; 40 | children = ( 41 | A2EB7727230075F700FD0C79 /* Example */, 42 | A2EB7726230075F700FD0C79 /* Products */, 43 | ); 44 | sourceTree = ""; 45 | }; 46 | A2EB7726230075F700FD0C79 /* Products */ = { 47 | isa = PBXGroup; 48 | children = ( 49 | A2EB7725230075F700FD0C79 /* Example.app */, 50 | ); 51 | name = Products; 52 | sourceTree = ""; 53 | }; 54 | A2EB7727230075F700FD0C79 /* Example */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | A2EB7728230075F700FD0C79 /* AppDelegate.swift */, 58 | A2EB772A230075F700FD0C79 /* ViewController.swift */, 59 | A2EB77412300809100FD0C79 /* PagingCollectionViewLayout.swift */, 60 | A2EB773B2300772B00FD0C79 /* Resources */, 61 | ); 62 | path = Example; 63 | sourceTree = ""; 64 | }; 65 | A2EB773B2300772B00FD0C79 /* Resources */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | A2EB772F230075F800FD0C79 /* Assets.xcassets */, 69 | A2EB7731230075F800FD0C79 /* LaunchScreen.storyboard */, 70 | A2EB7734230075F800FD0C79 /* Info.plist */, 71 | ); 72 | path = Resources; 73 | sourceTree = ""; 74 | }; 75 | /* End PBXGroup section */ 76 | 77 | /* Begin PBXNativeTarget section */ 78 | A2EB7724230075F700FD0C79 /* Example */ = { 79 | isa = PBXNativeTarget; 80 | buildConfigurationList = A2EB7737230075F800FD0C79 /* Build configuration list for PBXNativeTarget "Example" */; 81 | buildPhases = ( 82 | A2EB7721230075F700FD0C79 /* Sources */, 83 | A2EB7722230075F700FD0C79 /* Frameworks */, 84 | A2EB7723230075F700FD0C79 /* Resources */, 85 | ); 86 | buildRules = ( 87 | ); 88 | dependencies = ( 89 | ); 90 | name = Example; 91 | productName = Example; 92 | productReference = A2EB7725230075F700FD0C79 /* Example.app */; 93 | productType = "com.apple.product-type.application"; 94 | }; 95 | /* End PBXNativeTarget section */ 96 | 97 | /* Begin PBXProject section */ 98 | A2EB771D230075F700FD0C79 /* Project object */ = { 99 | isa = PBXProject; 100 | attributes = { 101 | LastSwiftUpdateCheck = 1020; 102 | LastUpgradeCheck = 1020; 103 | ORGANIZATIONNAME = "Alexander Kerendian"; 104 | TargetAttributes = { 105 | A2EB7724230075F700FD0C79 = { 106 | CreatedOnToolsVersion = 10.2.1; 107 | }; 108 | }; 109 | }; 110 | buildConfigurationList = A2EB7720230075F700FD0C79 /* Build configuration list for PBXProject "Example" */; 111 | compatibilityVersion = "Xcode 9.3"; 112 | developmentRegion = en; 113 | hasScannedForEncodings = 0; 114 | knownRegions = ( 115 | en, 116 | Base, 117 | ); 118 | mainGroup = A2EB771C230075F700FD0C79; 119 | productRefGroup = A2EB7726230075F700FD0C79 /* Products */; 120 | projectDirPath = ""; 121 | projectRoot = ""; 122 | targets = ( 123 | A2EB7724230075F700FD0C79 /* Example */, 124 | ); 125 | }; 126 | /* End PBXProject section */ 127 | 128 | /* Begin PBXResourcesBuildPhase section */ 129 | A2EB7723230075F700FD0C79 /* Resources */ = { 130 | isa = PBXResourcesBuildPhase; 131 | buildActionMask = 2147483647; 132 | files = ( 133 | A2EB7733230075F800FD0C79 /* LaunchScreen.storyboard in Resources */, 134 | A2EB7730230075F800FD0C79 /* Assets.xcassets in Resources */, 135 | ); 136 | runOnlyForDeploymentPostprocessing = 0; 137 | }; 138 | /* End PBXResourcesBuildPhase section */ 139 | 140 | /* Begin PBXSourcesBuildPhase section */ 141 | A2EB7721230075F700FD0C79 /* Sources */ = { 142 | isa = PBXSourcesBuildPhase; 143 | buildActionMask = 2147483647; 144 | files = ( 145 | A2EB772B230075F700FD0C79 /* ViewController.swift in Sources */, 146 | A2EB7729230075F700FD0C79 /* AppDelegate.swift in Sources */, 147 | A2EB77422300809100FD0C79 /* PagingCollectionViewLayout.swift in Sources */, 148 | ); 149 | runOnlyForDeploymentPostprocessing = 0; 150 | }; 151 | /* End PBXSourcesBuildPhase section */ 152 | 153 | /* Begin PBXVariantGroup section */ 154 | A2EB7731230075F800FD0C79 /* LaunchScreen.storyboard */ = { 155 | isa = PBXVariantGroup; 156 | children = ( 157 | A2EB7732230075F800FD0C79 /* Base */, 158 | ); 159 | name = LaunchScreen.storyboard; 160 | sourceTree = ""; 161 | }; 162 | /* End PBXVariantGroup section */ 163 | 164 | /* Begin XCBuildConfiguration section */ 165 | A2EB7735230075F800FD0C79 /* Debug */ = { 166 | isa = XCBuildConfiguration; 167 | buildSettings = { 168 | ALWAYS_SEARCH_USER_PATHS = NO; 169 | CLANG_ANALYZER_NONNULL = YES; 170 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 171 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 172 | CLANG_CXX_LIBRARY = "libc++"; 173 | CLANG_ENABLE_MODULES = YES; 174 | CLANG_ENABLE_OBJC_ARC = YES; 175 | CLANG_ENABLE_OBJC_WEAK = YES; 176 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 177 | CLANG_WARN_BOOL_CONVERSION = YES; 178 | CLANG_WARN_COMMA = YES; 179 | CLANG_WARN_CONSTANT_CONVERSION = YES; 180 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 181 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 182 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 183 | CLANG_WARN_EMPTY_BODY = YES; 184 | CLANG_WARN_ENUM_CONVERSION = YES; 185 | CLANG_WARN_INFINITE_RECURSION = YES; 186 | CLANG_WARN_INT_CONVERSION = YES; 187 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 188 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 189 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 190 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 191 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 192 | CLANG_WARN_STRICT_PROTOTYPES = YES; 193 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 194 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 195 | CLANG_WARN_UNREACHABLE_CODE = YES; 196 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 197 | CODE_SIGN_IDENTITY = "iPhone Developer"; 198 | COPY_PHASE_STRIP = NO; 199 | DEBUG_INFORMATION_FORMAT = dwarf; 200 | ENABLE_STRICT_OBJC_MSGSEND = YES; 201 | ENABLE_TESTABILITY = YES; 202 | GCC_C_LANGUAGE_STANDARD = gnu11; 203 | GCC_DYNAMIC_NO_PIC = NO; 204 | GCC_NO_COMMON_BLOCKS = YES; 205 | GCC_OPTIMIZATION_LEVEL = 0; 206 | GCC_PREPROCESSOR_DEFINITIONS = ( 207 | "DEBUG=1", 208 | "$(inherited)", 209 | ); 210 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 211 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 212 | GCC_WARN_UNDECLARED_SELECTOR = YES; 213 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 214 | GCC_WARN_UNUSED_FUNCTION = YES; 215 | GCC_WARN_UNUSED_VARIABLE = YES; 216 | IPHONEOS_DEPLOYMENT_TARGET = 12.2; 217 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 218 | MTL_FAST_MATH = YES; 219 | ONLY_ACTIVE_ARCH = YES; 220 | SDKROOT = iphoneos; 221 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 222 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 223 | }; 224 | name = Debug; 225 | }; 226 | A2EB7736230075F800FD0C79 /* Release */ = { 227 | isa = XCBuildConfiguration; 228 | buildSettings = { 229 | ALWAYS_SEARCH_USER_PATHS = NO; 230 | CLANG_ANALYZER_NONNULL = YES; 231 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 232 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 233 | CLANG_CXX_LIBRARY = "libc++"; 234 | CLANG_ENABLE_MODULES = YES; 235 | CLANG_ENABLE_OBJC_ARC = YES; 236 | CLANG_ENABLE_OBJC_WEAK = YES; 237 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 238 | CLANG_WARN_BOOL_CONVERSION = YES; 239 | CLANG_WARN_COMMA = YES; 240 | CLANG_WARN_CONSTANT_CONVERSION = YES; 241 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 242 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 243 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 244 | CLANG_WARN_EMPTY_BODY = YES; 245 | CLANG_WARN_ENUM_CONVERSION = YES; 246 | CLANG_WARN_INFINITE_RECURSION = YES; 247 | CLANG_WARN_INT_CONVERSION = YES; 248 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 249 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 250 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 251 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 252 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 253 | CLANG_WARN_STRICT_PROTOTYPES = YES; 254 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 255 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 256 | CLANG_WARN_UNREACHABLE_CODE = YES; 257 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 258 | CODE_SIGN_IDENTITY = "iPhone Developer"; 259 | COPY_PHASE_STRIP = NO; 260 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 261 | ENABLE_NS_ASSERTIONS = NO; 262 | ENABLE_STRICT_OBJC_MSGSEND = YES; 263 | GCC_C_LANGUAGE_STANDARD = gnu11; 264 | GCC_NO_COMMON_BLOCKS = YES; 265 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 266 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 267 | GCC_WARN_UNDECLARED_SELECTOR = YES; 268 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 269 | GCC_WARN_UNUSED_FUNCTION = YES; 270 | GCC_WARN_UNUSED_VARIABLE = YES; 271 | IPHONEOS_DEPLOYMENT_TARGET = 12.2; 272 | MTL_ENABLE_DEBUG_INFO = NO; 273 | MTL_FAST_MATH = YES; 274 | SDKROOT = iphoneos; 275 | SWIFT_COMPILATION_MODE = wholemodule; 276 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 277 | VALIDATE_PRODUCT = YES; 278 | }; 279 | name = Release; 280 | }; 281 | A2EB7738230075F800FD0C79 /* Debug */ = { 282 | isa = XCBuildConfiguration; 283 | buildSettings = { 284 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 285 | CODE_SIGN_STYLE = Automatic; 286 | DEVELOPMENT_TEAM = 6PQJ4KYE37; 287 | INFOPLIST_FILE = Example/Resources/Info.plist; 288 | LD_RUNPATH_SEARCH_PATHS = ( 289 | "$(inherited)", 290 | "@executable_path/Frameworks", 291 | ); 292 | PRODUCT_BUNDLE_IDENTIFIER = alexanderkerendian.Example; 293 | PRODUCT_NAME = "$(TARGET_NAME)"; 294 | SWIFT_VERSION = 5.0; 295 | TARGETED_DEVICE_FAMILY = "1,2"; 296 | }; 297 | name = Debug; 298 | }; 299 | A2EB7739230075F800FD0C79 /* Release */ = { 300 | isa = XCBuildConfiguration; 301 | buildSettings = { 302 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 303 | CODE_SIGN_STYLE = Automatic; 304 | DEVELOPMENT_TEAM = 6PQJ4KYE37; 305 | INFOPLIST_FILE = Example/Resources/Info.plist; 306 | LD_RUNPATH_SEARCH_PATHS = ( 307 | "$(inherited)", 308 | "@executable_path/Frameworks", 309 | ); 310 | PRODUCT_BUNDLE_IDENTIFIER = alexanderkerendian.Example; 311 | PRODUCT_NAME = "$(TARGET_NAME)"; 312 | SWIFT_VERSION = 5.0; 313 | TARGETED_DEVICE_FAMILY = "1,2"; 314 | }; 315 | name = Release; 316 | }; 317 | /* End XCBuildConfiguration section */ 318 | 319 | /* Begin XCConfigurationList section */ 320 | A2EB7720230075F700FD0C79 /* Build configuration list for PBXProject "Example" */ = { 321 | isa = XCConfigurationList; 322 | buildConfigurations = ( 323 | A2EB7735230075F800FD0C79 /* Debug */, 324 | A2EB7736230075F800FD0C79 /* Release */, 325 | ); 326 | defaultConfigurationIsVisible = 0; 327 | defaultConfigurationName = Release; 328 | }; 329 | A2EB7737230075F800FD0C79 /* Build configuration list for PBXNativeTarget "Example" */ = { 330 | isa = XCConfigurationList; 331 | buildConfigurations = ( 332 | A2EB7738230075F800FD0C79 /* Debug */, 333 | A2EB7739230075F800FD0C79 /* Release */, 334 | ); 335 | defaultConfigurationIsVisible = 0; 336 | defaultConfigurationName = Release; 337 | }; 338 | /* End XCConfigurationList section */ 339 | }; 340 | rootObject = A2EB771D230075F700FD0C79 /* Project object */; 341 | } 342 | -------------------------------------------------------------------------------- /Example/Example.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Example/Example.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Example/Example/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Example 4 | // 5 | // Created by Alex Kerendian on 8/11/19. 6 | // Copyright © 2019 Alexander Kerendian. 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 | 19 | window = UIWindow(frame: UIScreen.main.bounds) 20 | let viewController = ViewController() 21 | window?.rootViewController = viewController 22 | window?.makeKeyAndVisible() 23 | 24 | return true 25 | } 26 | 27 | func applicationWillResignActive(_ application: UIApplication) { 28 | // 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. 29 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 30 | } 31 | 32 | func applicationDidEnterBackground(_ application: UIApplication) { 33 | // 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. 34 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 35 | } 36 | 37 | func applicationWillEnterForeground(_ application: UIApplication) { 38 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 39 | } 40 | 41 | func applicationDidBecomeActive(_ application: UIApplication) { 42 | // 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. 43 | } 44 | 45 | func applicationWillTerminate(_ application: UIApplication) { 46 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 47 | } 48 | 49 | 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Example/Example/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /Example/Example/Resources/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /Example/Example/Resources/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 | -------------------------------------------------------------------------------- /Example/Example/Resources/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 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 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIRequiredDeviceCapabilities 26 | 27 | armv7 28 | 29 | UISupportedInterfaceOrientations 30 | 31 | UIInterfaceOrientationPortrait 32 | UIInterfaceOrientationLandscapeLeft 33 | UIInterfaceOrientationLandscapeRight 34 | 35 | UISupportedInterfaceOrientations~ipad 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationPortraitUpsideDown 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Example/Example/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Example 4 | // 5 | // Created by Alex Kerendian on 8/11/19. 6 | // Copyright © 2019 Alexander Kerendian. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | // MARK: - Constants 14 | 15 | let cellWidth = (3 / 4) * UIScreen.main.bounds.width 16 | let sectionSpacing = (1 / 8) * UIScreen.main.bounds.width 17 | let cellSpacing = (1 / 16) * UIScreen.main.bounds.width 18 | 19 | let colors: [UIColor] = [.red, .green, .blue, .purple, .orange, .black, .cyan] 20 | let cellId = "cell id" 21 | 22 | // MARK: - UI Components 23 | 24 | lazy var collectionView: UICollectionView = { 25 | let layout = PagingCollectionViewLayout() 26 | layout.scrollDirection = .horizontal 27 | layout.sectionInset = UIEdgeInsets(top: 0, left: sectionSpacing, bottom: 0, right: sectionSpacing) 28 | layout.itemSize = CGSize(width: cellWidth, height: cellWidth) 29 | layout.minimumLineSpacing = cellSpacing 30 | 31 | let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 32 | collectionView.translatesAutoresizingMaskIntoConstraints = false 33 | collectionView.showsHorizontalScrollIndicator = false 34 | collectionView.backgroundColor = .white 35 | collectionView.decelerationRate = .fast 36 | collectionView.dataSource = self 37 | return collectionView 38 | }() 39 | 40 | override func viewDidLoad() { 41 | super.viewDidLoad() 42 | 43 | design() 44 | registerCollectionViewCells() 45 | applyConstraints() 46 | } 47 | 48 | // MARK: - Setup 49 | 50 | private func design() { 51 | view.backgroundColor = .white 52 | } 53 | 54 | private func registerCollectionViewCells() { 55 | collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId) 56 | } 57 | 58 | private func applyConstraints() { 59 | view.addSubview(collectionView) 60 | collectionView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 61 | collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true 62 | collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true 63 | collectionView.heightAnchor.constraint(equalToConstant: cellWidth).isActive = true 64 | } 65 | } 66 | 67 | // MARK: - CollectionView Data Source 68 | 69 | extension ViewController: UICollectionViewDataSource { 70 | 71 | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 72 | return colors.count 73 | } 74 | 75 | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 76 | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) 77 | let color = colors[indexPath.item] 78 | cell.backgroundColor = color 79 | return cell 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 ak 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 | -------------------------------------------------------------------------------- /Media/example-multiple-items-per-page.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akxo/paging-collection-view-layout/f767362fc60ebbdf943f189116cd83df6de5115d/Media/example-multiple-items-per-page.gif -------------------------------------------------------------------------------- /Media/example-skip.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akxo/paging-collection-view-layout/f767362fc60ebbdf943f189116cd83df6de5115d/Media/example-skip.gif -------------------------------------------------------------------------------- /Media/example-vertical.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akxo/paging-collection-view-layout/f767362fc60ebbdf943f189116cd83df6de5115d/Media/example-vertical.gif -------------------------------------------------------------------------------- /Media/example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akxo/paging-collection-view-layout/f767362fc60ebbdf943f189116cd83df6de5115d/Media/example.gif -------------------------------------------------------------------------------- /Media/spacing-vertical.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akxo/paging-collection-view-layout/f767362fc60ebbdf943f189116cd83df6de5115d/Media/spacing-vertical.jpg -------------------------------------------------------------------------------- /Media/spacing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akxo/paging-collection-view-layout/f767362fc60ebbdf943f189116cd83df6de5115d/Media/spacing.png -------------------------------------------------------------------------------- /PagingCollectionViewLayout.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod spec lint PagingCollectionViewLayout.podspec' to ensure this is a 3 | # valid spec and to remove all comments including this before submitting the spec. 4 | # 5 | # To learn more about Podspec attributes see https://guides.cocoapods.org/syntax/podspec.html 6 | # To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ 7 | # 8 | 9 | Pod::Spec.new do |spec| 10 | 11 | # ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 12 | # 13 | # These will help people to find your library, and whilst it 14 | # can feel like a chore to fill in it's definitely to your advantage. The 15 | # summary should be tweet-length, and the description more in depth. 16 | # 17 | 18 | spec.name = "PagingCollectionViewLayout" 19 | spec.version = "0.0.4" 20 | spec.summary = "custom collection view layout" 21 | 22 | # This description is used to generate tags and improve search results. 23 | # * Think: What does it do? Why did you write it? What is the focus? 24 | # * Try to keep it short, snappy and to the point. 25 | # * Write the description between the DESC delimiters below. 26 | # * Finally, don't worry about the indent, CocoaPods strips it! 27 | spec.description = <<-DESC 28 | custom collection view layout that allows you to page by cell, not screen. 29 | DESC 30 | 31 | spec.homepage = "https://github.com/akxo/paging-collection-view-layout" 32 | spec.screenshots = "https://github.com/akxo/paging-collection-view-layout/blob/master/Media/example.gif?raw=true" 33 | 34 | 35 | # ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 36 | # 37 | # Licensing your code is important. See https://choosealicense.com for more info. 38 | # CocoaPods will detect a license file if there is a named LICENSE* 39 | # Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'. 40 | # 41 | 42 | spec.license = { :type => "MIT", :file => "LICENSE" } 43 | 44 | 45 | # ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 46 | # 47 | # Specify the authors of the library, with email addresses. Email addresses 48 | # of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also 49 | # accepts just a name if you'd rather not provide an email address. 50 | # 51 | # Specify a social_media_url where others can refer to, for example a twitter 52 | # profile URL. 53 | # 54 | 55 | spec.author = { "ak" => "atrix113@aol.com" } 56 | # spec.social_media_url = "https://twitter.com/ak" 57 | 58 | # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 59 | # 60 | # If this Pod runs only on iOS or OS X, then specify the platform and 61 | # the deployment target. You can optionally include the target after the platform. 62 | # 63 | 64 | spec.platform = :ios, "9.0" 65 | spec.swift_version = "5.0" 66 | 67 | 68 | # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 69 | # 70 | # Specify the location from where the source should be retrieved. 71 | # Supports git, hg, bzr, svn and HTTP. 72 | # 73 | 74 | spec.source = { :git => "https://github.com/akxo/paging-collection-view-layout.git", :tag => "#{spec.version}" } 75 | 76 | 77 | # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 78 | # 79 | # CocoaPods is smart about how it includes source code. For source files 80 | # giving a folder will include any swift, h, m, mm, c & cpp files. 81 | # For header files it will include any header in the folder. 82 | # Not including the public_header_files will make all headers public. 83 | # 84 | 85 | spec.source_files = "PagingCollectionViewLayout.swift" 86 | spec.exclude_files = "Example", "Media" 87 | 88 | # spec.public_header_files = "Classes/**/*.h" 89 | 90 | 91 | # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 92 | # 93 | # A list of resources included with the Pod. These are copied into the 94 | # target bundle with a build phase script. Anything else will be cleaned. 95 | # You can preserve files from being cleaned, please don't preserve 96 | # non-essential files like tests, examples and documentation. 97 | # 98 | 99 | # spec.resource = "icon.png" 100 | # spec.resources = "Resources/*.png" 101 | 102 | # spec.preserve_paths = "FilesToSave", "MoreFilesToSave" 103 | 104 | 105 | # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 106 | # 107 | # Link your library with frameworks, or libraries. Libraries do not include 108 | # the lib prefix of their name. 109 | # 110 | 111 | # spec.framework = "SomeFramework" 112 | # spec.frameworks = "SomeFramework", "AnotherFramework" 113 | 114 | # spec.library = "iconv" 115 | # spec.libraries = "iconv", "xml2" 116 | 117 | 118 | # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 119 | # 120 | # If your library depends on compiler flags you can set them in the xcconfig hash 121 | # where they will only apply to your library. If you depend on other Podspecs 122 | # you can include multiple dependencies to ensure it works. 123 | 124 | spec.requires_arc = true 125 | 126 | # spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" } 127 | # spec.dependency "JSONKit", "~> 1.4" 128 | 129 | end 130 | -------------------------------------------------------------------------------- /PagingCollectionViewLayout.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PagingCollectionViewLayout.swift 3 | // Example 4 | // 5 | // Created by Alex Kerendian on 8/11/19. 6 | // Copyright © 2019 Alexander Kerendian. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class PagingCollectionViewLayout: UICollectionViewFlowLayout { 12 | 13 | var velocityThresholdPerPage: CGFloat = 2 14 | var numberOfItemsPerPage: CGFloat = 1 15 | 16 | override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { 17 | guard let collectionView = collectionView else { return proposedContentOffset } 18 | 19 | let pageLength: CGFloat 20 | let approxPage: CGFloat 21 | let currentPage: CGFloat 22 | let speed: CGFloat 23 | 24 | if scrollDirection == .horizontal { 25 | pageLength = (self.itemSize.width + self.minimumLineSpacing) * numberOfItemsPerPage 26 | approxPage = collectionView.contentOffset.x / pageLength 27 | speed = velocity.x 28 | } else { 29 | pageLength = (self.itemSize.height + self.minimumLineSpacing) * numberOfItemsPerPage 30 | approxPage = collectionView.contentOffset.y / pageLength 31 | speed = velocity.y 32 | } 33 | 34 | if speed < 0 { 35 | currentPage = ceil(approxPage) 36 | } else if speed > 0 { 37 | currentPage = floor(approxPage) 38 | } else { 39 | currentPage = round(approxPage) 40 | } 41 | 42 | guard speed != 0 else { 43 | if scrollDirection == .horizontal { 44 | return CGPoint(x: currentPage * pageLength, y: 0) 45 | } else { 46 | return CGPoint(x: 0, y: currentPage * pageLength) 47 | } 48 | } 49 | 50 | var nextPage: CGFloat = currentPage + (speed > 0 ? 1 : -1) 51 | 52 | let increment = speed / velocityThresholdPerPage 53 | nextPage += (speed < 0) ? ceil(increment) : floor(increment) 54 | 55 | if scrollDirection == .horizontal { 56 | return CGPoint(x: nextPage * pageLength, y: 0) 57 | } else { 58 | return CGPoint(x: 0, y: nextPage * pageLength) 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # paging-collection-view-layout 2 | custom collection view layout that allows you to page by cell, not screen 3 | 4 | ![](Media/example.gif) 5 | ## installation 6 | ### cocoapods 7 | use [cocoapods](http://cocoapods.org/) to install `PagingCollectionViewLayout` by adding it to your `Podfile`: 8 | 9 | ```ruby 10 | platform :ios, '9.0' 11 | use_frameworks! 12 | pod 'PagingCollectionViewLayout' 13 | ``` 14 | 15 | ``` swift 16 | import PagingCollectionViewLayout 17 | ``` 18 | ### manual 19 | add `PagingCollectionViewLayout.swift` to your project 20 | ## usage 21 | 22 | instantiate a layout property 23 | 24 | ```swift 25 | let layout = PagingCollectionViewLayout() 26 | ``` 27 | set the following properties 28 | ```swift 29 | layout.itemSize = 30 | layout.minimumLineSpacing = 31 | layout.scrollDirection = .horizontal 32 | ``` 33 | instantiate a collection view using the layout property 34 | ```swift 35 | let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 36 | ``` 37 | for snappy scrolling set the following 38 | ```swift 39 | collectionView.decelerationRate = .fast 40 | ``` 41 | 42 | #### note: 43 | ~~collectionView.isPagingEnabled = true~~ 44 | ## optional customization 45 | ### vertical scrolling 46 | ![](Media/example-vertical.gif) 47 | ```swift 48 | layout.scrollDirection = .vertical 49 | ``` 50 | or since the default is vertical 51 | 52 | ~~layout.scrollDirection = .horizontal~~ 53 | ### section spacing 54 | - center the item in focus 55 | - allow the previous item to be visible 56 | 57 | ![](Media/spacing.png) 58 | ![](Media/spacing-vertical.jpg) 59 | 60 | set the following property 61 | ```swift 62 | layout.sectionInset = 63 | ``` 64 | - **horizontal collection view -** set left and right insets 65 | - **vertical collection view -** set top and bottom insets 66 | 67 | ### skipping items 68 | 69 | ![](Media/example-skip.gif) 70 | 71 | `layout.velocityThresholdPerPage` determines the velocity needed to skip an item (default = 2.0) 72 | - a non zero swipe velocity will result in the collection view scrolling to the prev/next item 73 | - the number of items skipped = swipe velocity / velocityThresholdPerPage 74 | 75 | ### multiple items per page 76 | 77 | ![](Media/example-multiple-items-per-page.gif) 78 | 79 | ```swift 80 | layout.numberOfItemsPerPage = 2 81 | ``` 82 | --------------------------------------------------------------------------------