├── CollectionLayout.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── benoit.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── benoit.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ └── xcschememanagement.plist ├── CollectionLayout ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── ColorDataSource.swift ├── CustomCollectionViewFlowLayout.swift ├── Info.plist ├── Model │ └── DynamicValue.swift └── ViewController.swift ├── LICENSE └── README.md /CollectionLayout.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 14C2D41C208D0B50001C1229 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14C2D41B208D0B50001C1229 /* AppDelegate.swift */; }; 11 | 14C2D41E208D0B50001C1229 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14C2D41D208D0B50001C1229 /* ViewController.swift */; }; 12 | 14C2D421208D0B50001C1229 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 14C2D41F208D0B50001C1229 /* Main.storyboard */; }; 13 | 14C2D423208D0B50001C1229 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 14C2D422208D0B50001C1229 /* Assets.xcassets */; }; 14 | 14C2D426208D0B50001C1229 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 14C2D424208D0B50001C1229 /* LaunchScreen.storyboard */; }; 15 | 14C2D42F208D0B8D001C1229 /* DynamicValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14C2D42E208D0B8D001C1229 /* DynamicValue.swift */; }; 16 | 14FB2C492091181C00FF00A9 /* ColorDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14FB2C482091181C00FF00A9 /* ColorDataSource.swift */; }; 17 | 14FB2C4B2091184600FF00A9 /* CustomCollectionViewFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14FB2C4A2091184600FF00A9 /* CustomCollectionViewFlowLayout.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | 14C2D418208D0B50001C1229 /* CollectionLayout.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CollectionLayout.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | 14C2D41B208D0B50001C1229 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 23 | 14C2D41D208D0B50001C1229 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 24 | 14C2D420208D0B50001C1229 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 25 | 14C2D422208D0B50001C1229 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 26 | 14C2D425208D0B50001C1229 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 27 | 14C2D427208D0B50001C1229 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 28 | 14C2D42E208D0B8D001C1229 /* DynamicValue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicValue.swift; sourceTree = ""; }; 29 | 14FB2C482091181C00FF00A9 /* ColorDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorDataSource.swift; sourceTree = ""; }; 30 | 14FB2C4A2091184600FF00A9 /* CustomCollectionViewFlowLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomCollectionViewFlowLayout.swift; sourceTree = ""; }; 31 | /* End PBXFileReference section */ 32 | 33 | /* Begin PBXFrameworksBuildPhase section */ 34 | 14C2D415208D0B50001C1229 /* Frameworks */ = { 35 | isa = PBXFrameworksBuildPhase; 36 | buildActionMask = 2147483647; 37 | files = ( 38 | ); 39 | runOnlyForDeploymentPostprocessing = 0; 40 | }; 41 | /* End PBXFrameworksBuildPhase section */ 42 | 43 | /* Begin PBXGroup section */ 44 | 14C2D40F208D0B4F001C1229 = { 45 | isa = PBXGroup; 46 | children = ( 47 | 14C2D41A208D0B50001C1229 /* CollectionLayout */, 48 | 14C2D419208D0B50001C1229 /* Products */, 49 | ); 50 | sourceTree = ""; 51 | }; 52 | 14C2D419208D0B50001C1229 /* Products */ = { 53 | isa = PBXGroup; 54 | children = ( 55 | 14C2D418208D0B50001C1229 /* CollectionLayout.app */, 56 | ); 57 | name = Products; 58 | sourceTree = ""; 59 | }; 60 | 14C2D41A208D0B50001C1229 /* CollectionLayout */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | 14C2D42D208D0B83001C1229 /* Model */, 64 | 14C2D41B208D0B50001C1229 /* AppDelegate.swift */, 65 | 14C2D41D208D0B50001C1229 /* ViewController.swift */, 66 | 14C2D41F208D0B50001C1229 /* Main.storyboard */, 67 | 14C2D422208D0B50001C1229 /* Assets.xcassets */, 68 | 14C2D424208D0B50001C1229 /* LaunchScreen.storyboard */, 69 | 14C2D427208D0B50001C1229 /* Info.plist */, 70 | 14FB2C482091181C00FF00A9 /* ColorDataSource.swift */, 71 | 14FB2C4A2091184600FF00A9 /* CustomCollectionViewFlowLayout.swift */, 72 | ); 73 | path = CollectionLayout; 74 | sourceTree = ""; 75 | }; 76 | 14C2D42D208D0B83001C1229 /* Model */ = { 77 | isa = PBXGroup; 78 | children = ( 79 | 14C2D42E208D0B8D001C1229 /* DynamicValue.swift */, 80 | ); 81 | path = Model; 82 | sourceTree = ""; 83 | }; 84 | /* End PBXGroup section */ 85 | 86 | /* Begin PBXNativeTarget section */ 87 | 14C2D417208D0B50001C1229 /* CollectionLayout */ = { 88 | isa = PBXNativeTarget; 89 | buildConfigurationList = 14C2D42A208D0B50001C1229 /* Build configuration list for PBXNativeTarget "CollectionLayout" */; 90 | buildPhases = ( 91 | 14C2D414208D0B50001C1229 /* Sources */, 92 | 14C2D415208D0B50001C1229 /* Frameworks */, 93 | 14C2D416208D0B50001C1229 /* Resources */, 94 | ); 95 | buildRules = ( 96 | ); 97 | dependencies = ( 98 | ); 99 | name = CollectionLayout; 100 | productName = CollectionLayout; 101 | productReference = 14C2D418208D0B50001C1229 /* CollectionLayout.app */; 102 | productType = "com.apple.product-type.application"; 103 | }; 104 | /* End PBXNativeTarget section */ 105 | 106 | /* Begin PBXProject section */ 107 | 14C2D410208D0B4F001C1229 /* Project object */ = { 108 | isa = PBXProject; 109 | attributes = { 110 | LastSwiftUpdateCheck = 0920; 111 | LastUpgradeCheck = 0920; 112 | ORGANIZATIONNAME = "Benoit PASQUIER"; 113 | TargetAttributes = { 114 | 14C2D417208D0B50001C1229 = { 115 | CreatedOnToolsVersion = 9.2; 116 | ProvisioningStyle = Automatic; 117 | }; 118 | }; 119 | }; 120 | buildConfigurationList = 14C2D413208D0B4F001C1229 /* Build configuration list for PBXProject "CollectionLayout" */; 121 | compatibilityVersion = "Xcode 8.0"; 122 | developmentRegion = en; 123 | hasScannedForEncodings = 0; 124 | knownRegions = ( 125 | en, 126 | Base, 127 | ); 128 | mainGroup = 14C2D40F208D0B4F001C1229; 129 | productRefGroup = 14C2D419208D0B50001C1229 /* Products */; 130 | projectDirPath = ""; 131 | projectRoot = ""; 132 | targets = ( 133 | 14C2D417208D0B50001C1229 /* CollectionLayout */, 134 | ); 135 | }; 136 | /* End PBXProject section */ 137 | 138 | /* Begin PBXResourcesBuildPhase section */ 139 | 14C2D416208D0B50001C1229 /* Resources */ = { 140 | isa = PBXResourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | 14C2D426208D0B50001C1229 /* LaunchScreen.storyboard in Resources */, 144 | 14C2D423208D0B50001C1229 /* Assets.xcassets in Resources */, 145 | 14C2D421208D0B50001C1229 /* Main.storyboard in Resources */, 146 | ); 147 | runOnlyForDeploymentPostprocessing = 0; 148 | }; 149 | /* End PBXResourcesBuildPhase section */ 150 | 151 | /* Begin PBXSourcesBuildPhase section */ 152 | 14C2D414208D0B50001C1229 /* Sources */ = { 153 | isa = PBXSourcesBuildPhase; 154 | buildActionMask = 2147483647; 155 | files = ( 156 | 14C2D41E208D0B50001C1229 /* ViewController.swift in Sources */, 157 | 14C2D42F208D0B8D001C1229 /* DynamicValue.swift in Sources */, 158 | 14FB2C492091181C00FF00A9 /* ColorDataSource.swift in Sources */, 159 | 14FB2C4B2091184600FF00A9 /* CustomCollectionViewFlowLayout.swift in Sources */, 160 | 14C2D41C208D0B50001C1229 /* AppDelegate.swift in Sources */, 161 | ); 162 | runOnlyForDeploymentPostprocessing = 0; 163 | }; 164 | /* End PBXSourcesBuildPhase section */ 165 | 166 | /* Begin PBXVariantGroup section */ 167 | 14C2D41F208D0B50001C1229 /* Main.storyboard */ = { 168 | isa = PBXVariantGroup; 169 | children = ( 170 | 14C2D420208D0B50001C1229 /* Base */, 171 | ); 172 | name = Main.storyboard; 173 | sourceTree = ""; 174 | }; 175 | 14C2D424208D0B50001C1229 /* LaunchScreen.storyboard */ = { 176 | isa = PBXVariantGroup; 177 | children = ( 178 | 14C2D425208D0B50001C1229 /* Base */, 179 | ); 180 | name = LaunchScreen.storyboard; 181 | sourceTree = ""; 182 | }; 183 | /* End PBXVariantGroup section */ 184 | 185 | /* Begin XCBuildConfiguration section */ 186 | 14C2D428208D0B50001C1229 /* Debug */ = { 187 | isa = XCBuildConfiguration; 188 | buildSettings = { 189 | ALWAYS_SEARCH_USER_PATHS = NO; 190 | CLANG_ANALYZER_NONNULL = YES; 191 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 192 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 193 | CLANG_CXX_LIBRARY = "libc++"; 194 | CLANG_ENABLE_MODULES = YES; 195 | CLANG_ENABLE_OBJC_ARC = YES; 196 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 197 | CLANG_WARN_BOOL_CONVERSION = YES; 198 | CLANG_WARN_COMMA = YES; 199 | CLANG_WARN_CONSTANT_CONVERSION = YES; 200 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 201 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 202 | CLANG_WARN_EMPTY_BODY = YES; 203 | CLANG_WARN_ENUM_CONVERSION = YES; 204 | CLANG_WARN_INFINITE_RECURSION = YES; 205 | CLANG_WARN_INT_CONVERSION = YES; 206 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 207 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 208 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 209 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 210 | CLANG_WARN_STRICT_PROTOTYPES = YES; 211 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 212 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 213 | CLANG_WARN_UNREACHABLE_CODE = YES; 214 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 215 | CODE_SIGN_IDENTITY = "iPhone Developer"; 216 | COPY_PHASE_STRIP = NO; 217 | DEBUG_INFORMATION_FORMAT = dwarf; 218 | ENABLE_STRICT_OBJC_MSGSEND = YES; 219 | ENABLE_TESTABILITY = YES; 220 | GCC_C_LANGUAGE_STANDARD = gnu11; 221 | GCC_DYNAMIC_NO_PIC = NO; 222 | GCC_NO_COMMON_BLOCKS = YES; 223 | GCC_OPTIMIZATION_LEVEL = 0; 224 | GCC_PREPROCESSOR_DEFINITIONS = ( 225 | "DEBUG=1", 226 | "$(inherited)", 227 | ); 228 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 229 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 230 | GCC_WARN_UNDECLARED_SELECTOR = YES; 231 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 232 | GCC_WARN_UNUSED_FUNCTION = YES; 233 | GCC_WARN_UNUSED_VARIABLE = YES; 234 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 235 | MTL_ENABLE_DEBUG_INFO = YES; 236 | ONLY_ACTIVE_ARCH = YES; 237 | SDKROOT = iphoneos; 238 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 239 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 240 | }; 241 | name = Debug; 242 | }; 243 | 14C2D429208D0B50001C1229 /* Release */ = { 244 | isa = XCBuildConfiguration; 245 | buildSettings = { 246 | ALWAYS_SEARCH_USER_PATHS = NO; 247 | CLANG_ANALYZER_NONNULL = YES; 248 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 249 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 250 | CLANG_CXX_LIBRARY = "libc++"; 251 | CLANG_ENABLE_MODULES = YES; 252 | CLANG_ENABLE_OBJC_ARC = YES; 253 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 254 | CLANG_WARN_BOOL_CONVERSION = YES; 255 | CLANG_WARN_COMMA = YES; 256 | CLANG_WARN_CONSTANT_CONVERSION = YES; 257 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 258 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 259 | CLANG_WARN_EMPTY_BODY = YES; 260 | CLANG_WARN_ENUM_CONVERSION = YES; 261 | CLANG_WARN_INFINITE_RECURSION = YES; 262 | CLANG_WARN_INT_CONVERSION = YES; 263 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 264 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 265 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 266 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 267 | CLANG_WARN_STRICT_PROTOTYPES = YES; 268 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 269 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 270 | CLANG_WARN_UNREACHABLE_CODE = YES; 271 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 272 | CODE_SIGN_IDENTITY = "iPhone Developer"; 273 | COPY_PHASE_STRIP = NO; 274 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 275 | ENABLE_NS_ASSERTIONS = NO; 276 | ENABLE_STRICT_OBJC_MSGSEND = YES; 277 | GCC_C_LANGUAGE_STANDARD = gnu11; 278 | GCC_NO_COMMON_BLOCKS = YES; 279 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 280 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 281 | GCC_WARN_UNDECLARED_SELECTOR = YES; 282 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 283 | GCC_WARN_UNUSED_FUNCTION = YES; 284 | GCC_WARN_UNUSED_VARIABLE = YES; 285 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 286 | MTL_ENABLE_DEBUG_INFO = NO; 287 | SDKROOT = iphoneos; 288 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 289 | VALIDATE_PRODUCT = YES; 290 | }; 291 | name = Release; 292 | }; 293 | 14C2D42B208D0B50001C1229 /* Debug */ = { 294 | isa = XCBuildConfiguration; 295 | buildSettings = { 296 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 297 | CODE_SIGN_STYLE = Automatic; 298 | DEVELOPMENT_TEAM = XG3URJ46HG; 299 | INFOPLIST_FILE = CollectionLayout/Info.plist; 300 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 301 | PRODUCT_BUNDLE_IDENTIFIER = fr.benoitpasquier.CollectionLayout; 302 | PRODUCT_NAME = "$(TARGET_NAME)"; 303 | SWIFT_VERSION = 4.0; 304 | TARGETED_DEVICE_FAMILY = "1,2"; 305 | }; 306 | name = Debug; 307 | }; 308 | 14C2D42C208D0B50001C1229 /* Release */ = { 309 | isa = XCBuildConfiguration; 310 | buildSettings = { 311 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 312 | CODE_SIGN_STYLE = Automatic; 313 | DEVELOPMENT_TEAM = XG3URJ46HG; 314 | INFOPLIST_FILE = CollectionLayout/Info.plist; 315 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 316 | PRODUCT_BUNDLE_IDENTIFIER = fr.benoitpasquier.CollectionLayout; 317 | PRODUCT_NAME = "$(TARGET_NAME)"; 318 | SWIFT_VERSION = 4.0; 319 | TARGETED_DEVICE_FAMILY = "1,2"; 320 | }; 321 | name = Release; 322 | }; 323 | /* End XCBuildConfiguration section */ 324 | 325 | /* Begin XCConfigurationList section */ 326 | 14C2D413208D0B4F001C1229 /* Build configuration list for PBXProject "CollectionLayout" */ = { 327 | isa = XCConfigurationList; 328 | buildConfigurations = ( 329 | 14C2D428208D0B50001C1229 /* Debug */, 330 | 14C2D429208D0B50001C1229 /* Release */, 331 | ); 332 | defaultConfigurationIsVisible = 0; 333 | defaultConfigurationName = Release; 334 | }; 335 | 14C2D42A208D0B50001C1229 /* Build configuration list for PBXNativeTarget "CollectionLayout" */ = { 336 | isa = XCConfigurationList; 337 | buildConfigurations = ( 338 | 14C2D42B208D0B50001C1229 /* Debug */, 339 | 14C2D42C208D0B50001C1229 /* Release */, 340 | ); 341 | defaultConfigurationIsVisible = 0; 342 | defaultConfigurationName = Release; 343 | }; 344 | /* End XCConfigurationList section */ 345 | }; 346 | rootObject = 14C2D410208D0B4F001C1229 /* Project object */; 347 | } 348 | -------------------------------------------------------------------------------- /CollectionLayout.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CollectionLayout.xcodeproj/project.xcworkspace/xcuserdata/benoit.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/popei69/UICollectionViewFlowLayout-Tutorial/f4327a6c8fa80bdacc94f5530a9453e622fdbe73/CollectionLayout.xcodeproj/project.xcworkspace/xcuserdata/benoit.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /CollectionLayout.xcodeproj/xcuserdata/benoit.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /CollectionLayout.xcodeproj/xcuserdata/benoit.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | CollectionLayout.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /CollectionLayout/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // CollectionLayout 4 | // 5 | // Created by Benoit PASQUIER on 22/04/2018. 6 | // Copyright © 2018 Benoit PASQUIER. 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: [UIApplicationLaunchOptionsKey: 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 invalidate graphics rendering callbacks. 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 active 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 | -------------------------------------------------------------------------------- /CollectionLayout/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 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /CollectionLayout/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 | -------------------------------------------------------------------------------- /CollectionLayout/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 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /CollectionLayout/ColorDataSource.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ColorDataSource.swift 3 | // CollectionLayout 4 | // 5 | // Created by Benoit PASQUIER on 25/04/2018. 6 | // Copyright © 2018 Benoit PASQUIER. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | class GenericDataSource : NSObject { 13 | var data: DynamicValue<[T]> = DynamicValue([]) 14 | } 15 | 16 | class ColorDataSource : GenericDataSource, UICollectionViewDataSource { 17 | 18 | func numberOfSections(in collectionView: UICollectionView) -> Int { 19 | return 1 20 | } 21 | 22 | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 23 | return data.value.count 24 | } 25 | 26 | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 27 | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ColorCell", for: indexPath) 28 | 29 | cell.backgroundColor = data.value[indexPath.row] 30 | return cell 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CollectionLayout/CustomCollectionViewFlowLayout.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CustomCollectionViewLayout.swift 3 | // CollectionLayout 4 | // 5 | // Created by Benoit PASQUIER on 25/04/2018. 6 | // Copyright © 2018 Benoit PASQUIER. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | enum CollectionDisplay { 13 | case inline 14 | case list 15 | case grid 16 | } 17 | 18 | class CustomCollectionViewFlowLayout : UICollectionViewFlowLayout { 19 | 20 | var display : CollectionDisplay = .grid { 21 | didSet { 22 | if display != oldValue { 23 | self.invalidateLayout() 24 | } 25 | } 26 | } 27 | 28 | convenience init(display: CollectionDisplay) { 29 | self.init() 30 | 31 | self.display = display 32 | self.minimumLineSpacing = 10 33 | self.minimumInteritemSpacing = 10 34 | self.configLayout() 35 | } 36 | 37 | func configLayout() { 38 | switch display { 39 | case .inline: 40 | self.scrollDirection = .horizontal 41 | if let collectionView = self.collectionView { 42 | self.itemSize = CGSize(width: collectionView.frame.width * 0.9, height: 300) 43 | } 44 | 45 | case .grid: 46 | 47 | self.scrollDirection = .vertical 48 | if let collectionView = self.collectionView { 49 | let optimisedWidth = (collectionView.frame.width - minimumInteritemSpacing) / 2 50 | self.itemSize = CGSize(width: optimisedWidth , height: optimisedWidth) // keep as square 51 | } 52 | 53 | case .list: 54 | self.scrollDirection = .vertical 55 | if let collectionView = self.collectionView { 56 | self.itemSize = CGSize(width: collectionView.frame.width , height: 130) 57 | } 58 | } 59 | } 60 | 61 | override func invalidateLayout() { 62 | super.invalidateLayout() 63 | self.configLayout() 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /CollectionLayout/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 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /CollectionLayout/Model/DynamicValue.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DynamicValue.swift 3 | // CollectionLayout 4 | // 5 | // Created by Benoit PASQUIER on 22/04/2018. 6 | // Copyright © 2018 Benoit PASQUIER. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | typealias CompletionHandler = (() -> Void) 12 | class DynamicValue { 13 | 14 | var value : T { 15 | didSet { 16 | self.notify() 17 | } 18 | } 19 | 20 | private var observers = [String: CompletionHandler]() 21 | 22 | init(_ value: T) { 23 | self.value = value 24 | } 25 | 26 | public func addObserver(_ observer: NSObject, completionHandler: @escaping CompletionHandler) { 27 | observers[observer.description] = completionHandler 28 | } 29 | 30 | public func addAndNotify(observer: NSObject, completionHandler: @escaping CompletionHandler) { 31 | self.addObserver(observer, completionHandler: completionHandler) 32 | self.notify() 33 | } 34 | 35 | private func notify() { 36 | observers.forEach({ $0.value() }) 37 | } 38 | 39 | deinit { 40 | observers.removeAll() 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /CollectionLayout/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // CollectionLayout 4 | // 5 | // Created by Benoit PASQUIER on 22/04/2018. 6 | // Copyright © 2018 Benoit PASQUIER. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | @IBOutlet weak var collectionView : UICollectionView! 14 | 15 | lazy var collectionViewFlowLayout : CustomCollectionViewFlowLayout = { 16 | let layout = CustomCollectionViewFlowLayout(display: .grid) 17 | return layout 18 | }() 19 | 20 | let dataSource = ColorDataSource() 21 | 22 | override func viewDidLoad() { 23 | super.viewDidLoad() 24 | // Do any additional setup after loading the view, typically from a nib. 25 | 26 | self.collectionView.collectionViewLayout = self.collectionViewFlowLayout 27 | self.collectionView.dataSource = self.dataSource 28 | 29 | self.dataSource.data.addAndNotify(observer: self) { [weak self] in 30 | self?.collectionView.reloadData() 31 | } 32 | 33 | self.dataSource.data.value = [.red, .orange, .cyan, .purple, .yellow, .magenta] 34 | } 35 | 36 | @IBAction func layoutValueChanged(_ sender: UISegmentedControl) { 37 | 38 | switch sender.selectedSegmentIndex { 39 | case 1: 40 | self.collectionViewFlowLayout.display = .list 41 | case 2: 42 | self.collectionViewFlowLayout.display = .inline 43 | default: 44 | self.collectionViewFlowLayout.display = .grid 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ben 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 | # UICollectionViewFlowLayout-Tutorial 2 | 3 | This sample app support a blog post explaining [how to optimise a UICollectionView implementation in Swift](https://benoitpasquier.com/optimise-uicollectionview-swift/). 4 | 5 | It introduces a way to standardise UICollectionView implementation relying on a custom UICollectionViewFlowLayout. 6 | This project follows MVVM architecture. 7 | 8 | _Like this project? Feel free to follow me on [Twitter](https://twitter.com/benoitpasquier_) to get the latest update._ 9 | 10 | --------------------------------------------------------------------------------