├── CollectionViewCustom.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── damienromito.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── CollectionViewCustom.xcscheme │ └── xcschememanagement.plist ├── CollectionViewCustom ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── CustomCollectionViewCell.swift ├── Info.plist └── ViewController.swift ├── README.md └── image.jpg /CollectionViewCustom.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1F57C6A61E4B62B40083FFC0 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F57C6A51E4B62B40083FFC0 /* AppDelegate.swift */; }; 11 | 1F57C6A81E4B62B40083FFC0 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F57C6A71E4B62B40083FFC0 /* ViewController.swift */; }; 12 | 1F57C6AB1E4B62B40083FFC0 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1F57C6A91E4B62B40083FFC0 /* Main.storyboard */; }; 13 | 1F57C6AD1E4B62B40083FFC0 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1F57C6AC1E4B62B40083FFC0 /* Assets.xcassets */; }; 14 | 1F57C6B01E4B62B40083FFC0 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1F57C6AE1E4B62B40083FFC0 /* LaunchScreen.storyboard */; }; 15 | 1F57C6B91E4B64D30083FFC0 /* CustomCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F57C6B71E4B64D30083FFC0 /* CustomCollectionViewCell.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 1F57C6A21E4B62B40083FFC0 /* CollectionViewCustom.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CollectionViewCustom.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 1F57C6A51E4B62B40083FFC0 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | 1F57C6A71E4B62B40083FFC0 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | 1F57C6AA1E4B62B40083FFC0 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | 1F57C6AC1E4B62B40083FFC0 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | 1F57C6AF1E4B62B40083FFC0 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | 1F57C6B11E4B62B40083FFC0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | 1F57C6B71E4B64D30083FFC0 /* CustomCollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomCollectionViewCell.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | 1F57C69F1E4B62B40083FFC0 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | 1F57C6991E4B62B40083FFC0 = { 41 | isa = PBXGroup; 42 | children = ( 43 | 1F57C6A41E4B62B40083FFC0 /* CollectionViewCustom */, 44 | 1F57C6A31E4B62B40083FFC0 /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | 1F57C6A31E4B62B40083FFC0 /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | 1F57C6A21E4B62B40083FFC0 /* CollectionViewCustom.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | 1F57C6A41E4B62B40083FFC0 /* CollectionViewCustom */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | 1F57C6A51E4B62B40083FFC0 /* AppDelegate.swift */, 60 | 1F57C6A71E4B62B40083FFC0 /* ViewController.swift */, 61 | 1F57C6A91E4B62B40083FFC0 /* Main.storyboard */, 62 | 1F57C6AC1E4B62B40083FFC0 /* Assets.xcassets */, 63 | 1F57C6AE1E4B62B40083FFC0 /* LaunchScreen.storyboard */, 64 | 1F57C6B11E4B62B40083FFC0 /* Info.plist */, 65 | 1F57C6B71E4B64D30083FFC0 /* CustomCollectionViewCell.swift */, 66 | ); 67 | path = CollectionViewCustom; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | 1F57C6A11E4B62B40083FFC0 /* CollectionViewCustom */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = 1F57C6B41E4B62B40083FFC0 /* Build configuration list for PBXNativeTarget "CollectionViewCustom" */; 76 | buildPhases = ( 77 | 1F57C69E1E4B62B40083FFC0 /* Sources */, 78 | 1F57C69F1E4B62B40083FFC0 /* Frameworks */, 79 | 1F57C6A01E4B62B40083FFC0 /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = CollectionViewCustom; 86 | productName = CollectionViewCustom; 87 | productReference = 1F57C6A21E4B62B40083FFC0 /* CollectionViewCustom.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | 1F57C69A1E4B62B40083FFC0 /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0820; 97 | LastUpgradeCheck = 0820; 98 | TargetAttributes = { 99 | 1F57C6A11E4B62B40083FFC0 = { 100 | CreatedOnToolsVersion = 8.2.1; 101 | DevelopmentTeam = 3L2YW8G94D; 102 | ProvisioningStyle = Automatic; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = 1F57C69D1E4B62B40083FFC0 /* Build configuration list for PBXProject "CollectionViewCustom" */; 107 | compatibilityVersion = "Xcode 3.2"; 108 | developmentRegion = English; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = 1F57C6991E4B62B40083FFC0; 115 | productRefGroup = 1F57C6A31E4B62B40083FFC0 /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | 1F57C6A11E4B62B40083FFC0 /* CollectionViewCustom */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXResourcesBuildPhase section */ 125 | 1F57C6A01E4B62B40083FFC0 /* Resources */ = { 126 | isa = PBXResourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | 1F57C6B01E4B62B40083FFC0 /* LaunchScreen.storyboard in Resources */, 130 | 1F57C6AD1E4B62B40083FFC0 /* Assets.xcassets in Resources */, 131 | 1F57C6AB1E4B62B40083FFC0 /* Main.storyboard in Resources */, 132 | ); 133 | runOnlyForDeploymentPostprocessing = 0; 134 | }; 135 | /* End PBXResourcesBuildPhase section */ 136 | 137 | /* Begin PBXSourcesBuildPhase section */ 138 | 1F57C69E1E4B62B40083FFC0 /* Sources */ = { 139 | isa = PBXSourcesBuildPhase; 140 | buildActionMask = 2147483647; 141 | files = ( 142 | 1F57C6A81E4B62B40083FFC0 /* ViewController.swift in Sources */, 143 | 1F57C6B91E4B64D30083FFC0 /* CustomCollectionViewCell.swift in Sources */, 144 | 1F57C6A61E4B62B40083FFC0 /* AppDelegate.swift in Sources */, 145 | ); 146 | runOnlyForDeploymentPostprocessing = 0; 147 | }; 148 | /* End PBXSourcesBuildPhase section */ 149 | 150 | /* Begin PBXVariantGroup section */ 151 | 1F57C6A91E4B62B40083FFC0 /* Main.storyboard */ = { 152 | isa = PBXVariantGroup; 153 | children = ( 154 | 1F57C6AA1E4B62B40083FFC0 /* Base */, 155 | ); 156 | name = Main.storyboard; 157 | sourceTree = ""; 158 | }; 159 | 1F57C6AE1E4B62B40083FFC0 /* LaunchScreen.storyboard */ = { 160 | isa = PBXVariantGroup; 161 | children = ( 162 | 1F57C6AF1E4B62B40083FFC0 /* Base */, 163 | ); 164 | name = LaunchScreen.storyboard; 165 | sourceTree = ""; 166 | }; 167 | /* End PBXVariantGroup section */ 168 | 169 | /* Begin XCBuildConfiguration section */ 170 | 1F57C6B21E4B62B40083FFC0 /* Debug */ = { 171 | isa = XCBuildConfiguration; 172 | buildSettings = { 173 | ALWAYS_SEARCH_USER_PATHS = NO; 174 | CLANG_ANALYZER_NONNULL = YES; 175 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 176 | CLANG_CXX_LIBRARY = "libc++"; 177 | CLANG_ENABLE_MODULES = YES; 178 | CLANG_ENABLE_OBJC_ARC = YES; 179 | CLANG_WARN_BOOL_CONVERSION = YES; 180 | CLANG_WARN_CONSTANT_CONVERSION = 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_OBJC_ROOT_CLASS = YES_ERROR; 188 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 189 | CLANG_WARN_UNREACHABLE_CODE = YES; 190 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 191 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 192 | COPY_PHASE_STRIP = NO; 193 | DEBUG_INFORMATION_FORMAT = dwarf; 194 | ENABLE_STRICT_OBJC_MSGSEND = YES; 195 | ENABLE_TESTABILITY = YES; 196 | GCC_C_LANGUAGE_STANDARD = gnu99; 197 | GCC_DYNAMIC_NO_PIC = NO; 198 | GCC_NO_COMMON_BLOCKS = YES; 199 | GCC_OPTIMIZATION_LEVEL = 0; 200 | GCC_PREPROCESSOR_DEFINITIONS = ( 201 | "DEBUG=1", 202 | "$(inherited)", 203 | ); 204 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 205 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 206 | GCC_WARN_UNDECLARED_SELECTOR = YES; 207 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 208 | GCC_WARN_UNUSED_FUNCTION = YES; 209 | GCC_WARN_UNUSED_VARIABLE = YES; 210 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 211 | MTL_ENABLE_DEBUG_INFO = YES; 212 | ONLY_ACTIVE_ARCH = YES; 213 | SDKROOT = iphoneos; 214 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 215 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 216 | }; 217 | name = Debug; 218 | }; 219 | 1F57C6B31E4B62B40083FFC0 /* Release */ = { 220 | isa = XCBuildConfiguration; 221 | buildSettings = { 222 | ALWAYS_SEARCH_USER_PATHS = NO; 223 | CLANG_ANALYZER_NONNULL = YES; 224 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 225 | CLANG_CXX_LIBRARY = "libc++"; 226 | CLANG_ENABLE_MODULES = YES; 227 | CLANG_ENABLE_OBJC_ARC = YES; 228 | CLANG_WARN_BOOL_CONVERSION = YES; 229 | CLANG_WARN_CONSTANT_CONVERSION = YES; 230 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 231 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 232 | CLANG_WARN_EMPTY_BODY = YES; 233 | CLANG_WARN_ENUM_CONVERSION = YES; 234 | CLANG_WARN_INFINITE_RECURSION = YES; 235 | CLANG_WARN_INT_CONVERSION = YES; 236 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 237 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 238 | CLANG_WARN_UNREACHABLE_CODE = YES; 239 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 240 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 241 | COPY_PHASE_STRIP = NO; 242 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 243 | ENABLE_NS_ASSERTIONS = NO; 244 | ENABLE_STRICT_OBJC_MSGSEND = YES; 245 | GCC_C_LANGUAGE_STANDARD = gnu99; 246 | GCC_NO_COMMON_BLOCKS = YES; 247 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 248 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 249 | GCC_WARN_UNDECLARED_SELECTOR = YES; 250 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 251 | GCC_WARN_UNUSED_FUNCTION = YES; 252 | GCC_WARN_UNUSED_VARIABLE = YES; 253 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 254 | MTL_ENABLE_DEBUG_INFO = NO; 255 | SDKROOT = iphoneos; 256 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 257 | VALIDATE_PRODUCT = YES; 258 | }; 259 | name = Release; 260 | }; 261 | 1F57C6B51E4B62B40083FFC0 /* Debug */ = { 262 | isa = XCBuildConfiguration; 263 | buildSettings = { 264 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 265 | DEVELOPMENT_TEAM = 3L2YW8G94D; 266 | INFOPLIST_FILE = CollectionViewCustom/Info.plist; 267 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 268 | PRODUCT_BUNDLE_IDENTIFIER = fr.romito.CollectionViewCustom; 269 | PRODUCT_NAME = "$(TARGET_NAME)"; 270 | SWIFT_VERSION = 3.0; 271 | }; 272 | name = Debug; 273 | }; 274 | 1F57C6B61E4B62B40083FFC0 /* Release */ = { 275 | isa = XCBuildConfiguration; 276 | buildSettings = { 277 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 278 | DEVELOPMENT_TEAM = 3L2YW8G94D; 279 | INFOPLIST_FILE = CollectionViewCustom/Info.plist; 280 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 281 | PRODUCT_BUNDLE_IDENTIFIER = fr.romito.CollectionViewCustom; 282 | PRODUCT_NAME = "$(TARGET_NAME)"; 283 | SWIFT_VERSION = 3.0; 284 | }; 285 | name = Release; 286 | }; 287 | /* End XCBuildConfiguration section */ 288 | 289 | /* Begin XCConfigurationList section */ 290 | 1F57C69D1E4B62B40083FFC0 /* Build configuration list for PBXProject "CollectionViewCustom" */ = { 291 | isa = XCConfigurationList; 292 | buildConfigurations = ( 293 | 1F57C6B21E4B62B40083FFC0 /* Debug */, 294 | 1F57C6B31E4B62B40083FFC0 /* Release */, 295 | ); 296 | defaultConfigurationIsVisible = 0; 297 | defaultConfigurationName = Release; 298 | }; 299 | 1F57C6B41E4B62B40083FFC0 /* Build configuration list for PBXNativeTarget "CollectionViewCustom" */ = { 300 | isa = XCConfigurationList; 301 | buildConfigurations = ( 302 | 1F57C6B51E4B62B40083FFC0 /* Debug */, 303 | 1F57C6B61E4B62B40083FFC0 /* Release */, 304 | ); 305 | defaultConfigurationIsVisible = 0; 306 | }; 307 | /* End XCConfigurationList section */ 308 | }; 309 | rootObject = 1F57C69A1E4B62B40083FFC0 /* Project object */; 310 | } 311 | -------------------------------------------------------------------------------- /CollectionViewCustom.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CollectionViewCustom.xcodeproj/xcuserdata/damienromito.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /CollectionViewCustom.xcodeproj/xcuserdata/damienromito.xcuserdatad/xcschemes/CollectionViewCustom.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /CollectionViewCustom.xcodeproj/xcuserdata/damienromito.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | CollectionViewCustom.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 1F57C6A11E4B62B40083FFC0 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /CollectionViewCustom/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // CollectionViewCustom 4 | // 5 | // Created by Damien Romito on 08/02/2017. 6 | // 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 | -------------------------------------------------------------------------------- /CollectionViewCustom/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 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /CollectionViewCustom/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 | -------------------------------------------------------------------------------- /CollectionViewCustom/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 | 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 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /CollectionViewCustom/CustomCollectionViewCell.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CustomCollectionViewCell.swift 3 | // CollectionViewCustom 4 | // 5 | // Created by Damien Romito on 08/02/2017. 6 | // 7 | // 8 | 9 | import UIKit 10 | 11 | class CustomCollectionViewCell: UICollectionViewCell { 12 | 13 | @IBOutlet weak var titleLabel: UILabel! 14 | override func awakeFromNib() { 15 | super.awakeFromNib() 16 | // Initialization code 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 | -------------------------------------------------------------------------------- /CollectionViewCustom/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 | 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 | 38 | 39 | -------------------------------------------------------------------------------- /CollectionViewCustom/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // CollectionViewCustom 4 | // 5 | // Created by Damien Romito on 08/02/2017. 6 | // 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate { 12 | 13 | @IBOutlet weak var pageControl: UIPageControl! 14 | @IBOutlet weak var collectionView: UICollectionView! 15 | let reuseIdentifier = "CustomCell" 16 | let collectionMargin = CGFloat(16) 17 | let itemSpacing = CGFloat(10) 18 | let itemHeight = CGFloat(322) 19 | 20 | var itemWidth = CGFloat(0) 21 | var currentItem = 0 22 | 23 | override func viewDidLoad() { 24 | super.viewDidLoad() 25 | // Do any additional setup after loading the view, typically from a nib. 26 | setup() 27 | 28 | } 29 | 30 | override func didReceiveMemoryWarning() { 31 | super.didReceiveMemoryWarning() 32 | // Dispose of any resources that can be recreated. 33 | } 34 | 35 | override func awakeFromNib() { 36 | super.awakeFromNib() 37 | } 38 | 39 | var items = ["Item 1 ", "Item 2 ", "Item 3 "] 40 | 41 | 42 | 43 | func setup() { 44 | // 45 | // let bundle = Bundle(for: CustomCollectionViewCell.self) 46 | // let nib = UINib(nibName: String(describing: CustomCollectionViewCell.self), bundle:bundle) 47 | // self.collectionView.register(nib, forCellWithReuseIdentifier: reuseIdentifier) 48 | 49 | 50 | let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 51 | 52 | itemWidth = UIScreen.main.bounds.width - collectionMargin * 2.0 53 | 54 | layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 55 | layout.itemSize = CGSize(width: itemWidth, height: itemHeight) 56 | layout.headerReferenceSize = CGSize(width: collectionMargin, height: 0) 57 | layout.footerReferenceSize = CGSize(width: collectionMargin, height: 0) 58 | 59 | layout.minimumLineSpacing = itemSpacing 60 | layout.scrollDirection = .horizontal 61 | collectionView!.collectionViewLayout = layout 62 | collectionView?.decelerationRate = UIScrollViewDecelerationRateFast 63 | 64 | 65 | } 66 | 67 | func numberOfSections(in collectionView: UICollectionView) -> Int { 68 | return 1 69 | } 70 | 71 | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 72 | self.pageControl.numberOfPages = items.count 73 | return items.count 74 | } 75 | 76 | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 77 | 78 | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, 79 | for: indexPath) as! CustomCollectionViewCell 80 | 81 | // Configure the cell 82 | cell.titleLabel.text = items[indexPath.row] 83 | 84 | return cell 85 | } 86 | 87 | 88 | // MARK: - UIScrollViewDelegate protocol 89 | 90 | func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { 91 | 92 | let pageWidth = Float(itemWidth + itemSpacing) 93 | let targetXContentOffset = Float(targetContentOffset.pointee.x) 94 | let contentWidth = Float(collectionView!.contentSize.width ) 95 | var newPage = Float(self.pageControl.currentPage) 96 | 97 | if velocity.x == 0 { 98 | newPage = floor( (targetXContentOffset - Float(pageWidth) / 2) / Float(pageWidth)) + 1.0 99 | } else { 100 | newPage = Float(velocity.x > 0 ? self.pageControl.currentPage + 1 : self.pageControl.currentPage - 1) 101 | if newPage < 0 { 102 | newPage = 0 103 | } 104 | if (newPage > contentWidth / pageWidth) { 105 | newPage = ceil(contentWidth / pageWidth) - 1.0 106 | } 107 | } 108 | 109 | self.pageControl.currentPage = Int(newPage) 110 | let point = CGPoint (x: CGFloat(newPage * pageWidth), y: targetContentOffset.pointee.y) 111 | targetContentOffset.pointee = point 112 | } 113 | 114 | } 115 | 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | #Custom UICollectionView 4 | 5 | *Create custom UICollectionViewLayoutWith with a custom paging size of UICollectionViewCell by overriding scrollViewWillEndDragging* 6 | 7 | ![alt text](https://github.com/damienromito/CollectionViewCustom/blob/master/image.jpg "custom UICollectionViewCell size") 8 | 9 | ```swift 10 | let collectionMargin = CGFloat(16) 11 | let itemSpacing = CGFloat(10) 12 | let itemHeight = CGFloat(322) 13 | var itemWidth = CGFloat(0) 14 | var currentItem = 0 15 | ``` 16 | 17 | ## 1 Custom CollectionViewLayout 18 | 19 | ```swift 20 | override func viewDidLoad() { 21 | super.viewDidLoad() 22 | 23 | let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 24 | 25 | itemWidth = UIScreen.main.bounds.width - collectionMargin * 2.0 26 | 27 | layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 28 | layout.itemSize = CGSize(width: itemWidth, height: itemHeight) 29 | layout.headerReferenceSize = CGSize(width: collectionMargin, height: 0) 30 | layout.footerReferenceSize = CGSize(width: collectionMargin, height: 0) 31 | layout.minimumLineSpacing = itemSpacing 32 | layout.scrollDirection = .horizontal 33 | 34 | collectionView!.collectionViewLayout = layout 35 | collectionView?.decelerationRate = UIScrollViewDecelerationRateFast 36 | 37 | } 38 | ``` 39 | 40 | 41 | ## 2 Custom Paging size 42 | 43 | ```swift 44 | func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { 45 | 46 | let pageWidth = Float(itemWidth + itemSpacing) 47 | let targetXContentOffset = Float(targetContentOffset.pointee.x) 48 | let contentWidth = Float(collectionView!.contentSize.width ) 49 | var newPage = Float(self.pageControl.currentPage) 50 | 51 | if velocity.x == 0 { 52 | newPage = floor( (targetXContentOffset - Float(pageWidth) / 2) / Float(pageWidth)) + 1.0 53 | } else { 54 | newPage = Float(velocity.x > 0 ? self.pageControl.currentPage + 1 : self.pageControl.currentPage - 1) 55 | if newPage < 0 { 56 | newPage = 0 57 | } 58 | if (newPage > contentWidth / pageWidth) { 59 | newPage = ceil(contentWidth / pageWidth) - 1.0 60 | } 61 | } 62 | self.pageControl.currentPage = Int(newPage) 63 | let point = CGPoint (x: CGFloat(newPage * pageWidth), y: targetContentOffset.pointee.y) 64 | targetContentOffset.pointee = point 65 | } 66 | ``` 67 | -------------------------------------------------------------------------------- /image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/damienromito/CollectionViewCustom/077d533cb5114da706a575ca88af8a3f93dd55c3/image.jpg --------------------------------------------------------------------------------