├── CollectionViewGradientSample.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── xcuserdata │ │ └── payal.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── payal.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist ├── CollectionViewGradientSample ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── CustomCollectionViewCell.swift ├── Info.plist └── ViewController.swift ├── README.md └── Screenshot.gif /CollectionViewGradientSample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | DF7244CB20A8B572008A4B84 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF7244CA20A8B572008A4B84 /* AppDelegate.swift */; }; 11 | DF7244CD20A8B572008A4B84 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF7244CC20A8B572008A4B84 /* ViewController.swift */; }; 12 | DF7244D020A8B572008A4B84 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DF7244CE20A8B572008A4B84 /* Main.storyboard */; }; 13 | DF7244D220A8B574008A4B84 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DF7244D120A8B574008A4B84 /* Assets.xcassets */; }; 14 | DF7244D520A8B574008A4B84 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DF7244D320A8B574008A4B84 /* LaunchScreen.storyboard */; }; 15 | DF7244DD20A8B680008A4B84 /* CustomCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF7244DC20A8B680008A4B84 /* CustomCollectionViewCell.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | DF7244C720A8B572008A4B84 /* CollectionViewGradientSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CollectionViewGradientSample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | DF7244CA20A8B572008A4B84 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | DF7244CC20A8B572008A4B84 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | DF7244CF20A8B572008A4B84 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | DF7244D120A8B574008A4B84 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | DF7244D420A8B574008A4B84 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | DF7244D620A8B574008A4B84 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | DF7244DC20A8B680008A4B84 /* CustomCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomCollectionViewCell.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | DF7244C420A8B572008A4B84 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | DF7244BE20A8B572008A4B84 = { 41 | isa = PBXGroup; 42 | children = ( 43 | DF7244C920A8B572008A4B84 /* CollectionViewGradientSample */, 44 | DF7244C820A8B572008A4B84 /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | DF7244C820A8B572008A4B84 /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | DF7244C720A8B572008A4B84 /* CollectionViewGradientSample.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | DF7244C920A8B572008A4B84 /* CollectionViewGradientSample */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | DF7244CA20A8B572008A4B84 /* AppDelegate.swift */, 60 | DF7244CC20A8B572008A4B84 /* ViewController.swift */, 61 | DF7244DC20A8B680008A4B84 /* CustomCollectionViewCell.swift */, 62 | DF7244CE20A8B572008A4B84 /* Main.storyboard */, 63 | DF7244D120A8B574008A4B84 /* Assets.xcassets */, 64 | DF7244D320A8B574008A4B84 /* LaunchScreen.storyboard */, 65 | DF7244D620A8B574008A4B84 /* Info.plist */, 66 | ); 67 | path = CollectionViewGradientSample; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | DF7244C620A8B572008A4B84 /* CollectionViewGradientSample */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = DF7244D920A8B574008A4B84 /* Build configuration list for PBXNativeTarget "CollectionViewGradientSample" */; 76 | buildPhases = ( 77 | DF7244C320A8B572008A4B84 /* Sources */, 78 | DF7244C420A8B572008A4B84 /* Frameworks */, 79 | DF7244C520A8B572008A4B84 /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = CollectionViewGradientSample; 86 | productName = CollectionViewGradientSample; 87 | productReference = DF7244C720A8B572008A4B84 /* CollectionViewGradientSample.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | DF7244BF20A8B572008A4B84 /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0930; 97 | LastUpgradeCheck = 0930; 98 | ORGANIZATIONNAME = "Payal Gupta"; 99 | TargetAttributes = { 100 | DF7244C620A8B572008A4B84 = { 101 | CreatedOnToolsVersion = 9.3; 102 | }; 103 | }; 104 | }; 105 | buildConfigurationList = DF7244C220A8B572008A4B84 /* Build configuration list for PBXProject "CollectionViewGradientSample" */; 106 | compatibilityVersion = "Xcode 9.3"; 107 | developmentRegion = en; 108 | hasScannedForEncodings = 0; 109 | knownRegions = ( 110 | en, 111 | Base, 112 | ); 113 | mainGroup = DF7244BE20A8B572008A4B84; 114 | productRefGroup = DF7244C820A8B572008A4B84 /* Products */; 115 | projectDirPath = ""; 116 | projectRoot = ""; 117 | targets = ( 118 | DF7244C620A8B572008A4B84 /* CollectionViewGradientSample */, 119 | ); 120 | }; 121 | /* End PBXProject section */ 122 | 123 | /* Begin PBXResourcesBuildPhase section */ 124 | DF7244C520A8B572008A4B84 /* Resources */ = { 125 | isa = PBXResourcesBuildPhase; 126 | buildActionMask = 2147483647; 127 | files = ( 128 | DF7244D520A8B574008A4B84 /* LaunchScreen.storyboard in Resources */, 129 | DF7244D220A8B574008A4B84 /* Assets.xcassets in Resources */, 130 | DF7244D020A8B572008A4B84 /* Main.storyboard in Resources */, 131 | ); 132 | runOnlyForDeploymentPostprocessing = 0; 133 | }; 134 | /* End PBXResourcesBuildPhase section */ 135 | 136 | /* Begin PBXSourcesBuildPhase section */ 137 | DF7244C320A8B572008A4B84 /* Sources */ = { 138 | isa = PBXSourcesBuildPhase; 139 | buildActionMask = 2147483647; 140 | files = ( 141 | DF7244CD20A8B572008A4B84 /* ViewController.swift in Sources */, 142 | DF7244DD20A8B680008A4B84 /* CustomCollectionViewCell.swift in Sources */, 143 | DF7244CB20A8B572008A4B84 /* AppDelegate.swift in Sources */, 144 | ); 145 | runOnlyForDeploymentPostprocessing = 0; 146 | }; 147 | /* End PBXSourcesBuildPhase section */ 148 | 149 | /* Begin PBXVariantGroup section */ 150 | DF7244CE20A8B572008A4B84 /* Main.storyboard */ = { 151 | isa = PBXVariantGroup; 152 | children = ( 153 | DF7244CF20A8B572008A4B84 /* Base */, 154 | ); 155 | name = Main.storyboard; 156 | sourceTree = ""; 157 | }; 158 | DF7244D320A8B574008A4B84 /* LaunchScreen.storyboard */ = { 159 | isa = PBXVariantGroup; 160 | children = ( 161 | DF7244D420A8B574008A4B84 /* Base */, 162 | ); 163 | name = LaunchScreen.storyboard; 164 | sourceTree = ""; 165 | }; 166 | /* End PBXVariantGroup section */ 167 | 168 | /* Begin XCBuildConfiguration section */ 169 | DF7244D720A8B574008A4B84 /* Debug */ = { 170 | isa = XCBuildConfiguration; 171 | buildSettings = { 172 | ALWAYS_SEARCH_USER_PATHS = NO; 173 | CLANG_ANALYZER_NONNULL = YES; 174 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 175 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 176 | CLANG_CXX_LIBRARY = "libc++"; 177 | CLANG_ENABLE_MODULES = YES; 178 | CLANG_ENABLE_OBJC_ARC = YES; 179 | CLANG_ENABLE_OBJC_WEAK = YES; 180 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 181 | CLANG_WARN_BOOL_CONVERSION = YES; 182 | CLANG_WARN_COMMA = YES; 183 | CLANG_WARN_CONSTANT_CONVERSION = YES; 184 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 185 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 186 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 187 | CLANG_WARN_EMPTY_BODY = YES; 188 | CLANG_WARN_ENUM_CONVERSION = YES; 189 | CLANG_WARN_INFINITE_RECURSION = YES; 190 | CLANG_WARN_INT_CONVERSION = YES; 191 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 192 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 193 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 194 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 195 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 196 | CLANG_WARN_STRICT_PROTOTYPES = YES; 197 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 198 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 199 | CLANG_WARN_UNREACHABLE_CODE = YES; 200 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 201 | CODE_SIGN_IDENTITY = "iPhone Developer"; 202 | COPY_PHASE_STRIP = NO; 203 | DEBUG_INFORMATION_FORMAT = dwarf; 204 | ENABLE_STRICT_OBJC_MSGSEND = YES; 205 | ENABLE_TESTABILITY = YES; 206 | GCC_C_LANGUAGE_STANDARD = gnu11; 207 | GCC_DYNAMIC_NO_PIC = NO; 208 | GCC_NO_COMMON_BLOCKS = YES; 209 | GCC_OPTIMIZATION_LEVEL = 0; 210 | GCC_PREPROCESSOR_DEFINITIONS = ( 211 | "DEBUG=1", 212 | "$(inherited)", 213 | ); 214 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 215 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 216 | GCC_WARN_UNDECLARED_SELECTOR = YES; 217 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 218 | GCC_WARN_UNUSED_FUNCTION = YES; 219 | GCC_WARN_UNUSED_VARIABLE = YES; 220 | IPHONEOS_DEPLOYMENT_TARGET = 11.3; 221 | MTL_ENABLE_DEBUG_INFO = YES; 222 | ONLY_ACTIVE_ARCH = YES; 223 | SDKROOT = iphoneos; 224 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 225 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 226 | }; 227 | name = Debug; 228 | }; 229 | DF7244D820A8B574008A4B84 /* Release */ = { 230 | isa = XCBuildConfiguration; 231 | buildSettings = { 232 | ALWAYS_SEARCH_USER_PATHS = NO; 233 | CLANG_ANALYZER_NONNULL = YES; 234 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 235 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 236 | CLANG_CXX_LIBRARY = "libc++"; 237 | CLANG_ENABLE_MODULES = YES; 238 | CLANG_ENABLE_OBJC_ARC = YES; 239 | CLANG_ENABLE_OBJC_WEAK = YES; 240 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 241 | CLANG_WARN_BOOL_CONVERSION = YES; 242 | CLANG_WARN_COMMA = YES; 243 | CLANG_WARN_CONSTANT_CONVERSION = YES; 244 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 245 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 246 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 247 | CLANG_WARN_EMPTY_BODY = YES; 248 | CLANG_WARN_ENUM_CONVERSION = YES; 249 | CLANG_WARN_INFINITE_RECURSION = YES; 250 | CLANG_WARN_INT_CONVERSION = YES; 251 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 252 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 253 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 254 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 255 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 256 | CLANG_WARN_STRICT_PROTOTYPES = YES; 257 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 258 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 259 | CLANG_WARN_UNREACHABLE_CODE = YES; 260 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 261 | CODE_SIGN_IDENTITY = "iPhone Developer"; 262 | COPY_PHASE_STRIP = NO; 263 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 264 | ENABLE_NS_ASSERTIONS = NO; 265 | ENABLE_STRICT_OBJC_MSGSEND = YES; 266 | GCC_C_LANGUAGE_STANDARD = gnu11; 267 | GCC_NO_COMMON_BLOCKS = YES; 268 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 269 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 270 | GCC_WARN_UNDECLARED_SELECTOR = YES; 271 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 272 | GCC_WARN_UNUSED_FUNCTION = YES; 273 | GCC_WARN_UNUSED_VARIABLE = YES; 274 | IPHONEOS_DEPLOYMENT_TARGET = 11.3; 275 | MTL_ENABLE_DEBUG_INFO = NO; 276 | SDKROOT = iphoneos; 277 | SWIFT_COMPILATION_MODE = wholemodule; 278 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 279 | VALIDATE_PRODUCT = YES; 280 | }; 281 | name = Release; 282 | }; 283 | DF7244DA20A8B574008A4B84 /* Debug */ = { 284 | isa = XCBuildConfiguration; 285 | buildSettings = { 286 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 287 | CODE_SIGN_STYLE = Automatic; 288 | INFOPLIST_FILE = CollectionViewGradientSample/Info.plist; 289 | LD_RUNPATH_SEARCH_PATHS = ( 290 | "$(inherited)", 291 | "@executable_path/Frameworks", 292 | ); 293 | PRODUCT_BUNDLE_IDENTIFIER = com.payal.CollectionViewGradientSample; 294 | PRODUCT_NAME = "$(TARGET_NAME)"; 295 | SWIFT_VERSION = 4.0; 296 | TARGETED_DEVICE_FAMILY = "1,2"; 297 | }; 298 | name = Debug; 299 | }; 300 | DF7244DB20A8B574008A4B84 /* Release */ = { 301 | isa = XCBuildConfiguration; 302 | buildSettings = { 303 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 304 | CODE_SIGN_STYLE = Automatic; 305 | INFOPLIST_FILE = CollectionViewGradientSample/Info.plist; 306 | LD_RUNPATH_SEARCH_PATHS = ( 307 | "$(inherited)", 308 | "@executable_path/Frameworks", 309 | ); 310 | PRODUCT_BUNDLE_IDENTIFIER = com.payal.CollectionViewGradientSample; 311 | PRODUCT_NAME = "$(TARGET_NAME)"; 312 | SWIFT_VERSION = 4.0; 313 | TARGETED_DEVICE_FAMILY = "1,2"; 314 | }; 315 | name = Release; 316 | }; 317 | /* End XCBuildConfiguration section */ 318 | 319 | /* Begin XCConfigurationList section */ 320 | DF7244C220A8B572008A4B84 /* Build configuration list for PBXProject "CollectionViewGradientSample" */ = { 321 | isa = XCConfigurationList; 322 | buildConfigurations = ( 323 | DF7244D720A8B574008A4B84 /* Debug */, 324 | DF7244D820A8B574008A4B84 /* Release */, 325 | ); 326 | defaultConfigurationIsVisible = 0; 327 | defaultConfigurationName = Release; 328 | }; 329 | DF7244D920A8B574008A4B84 /* Build configuration list for PBXNativeTarget "CollectionViewGradientSample" */ = { 330 | isa = XCConfigurationList; 331 | buildConfigurations = ( 332 | DF7244DA20A8B574008A4B84 /* Debug */, 333 | DF7244DB20A8B574008A4B84 /* Release */, 334 | ); 335 | defaultConfigurationIsVisible = 0; 336 | defaultConfigurationName = Release; 337 | }; 338 | /* End XCConfigurationList section */ 339 | }; 340 | rootObject = DF7244BF20A8B572008A4B84 /* Project object */; 341 | } 342 | -------------------------------------------------------------------------------- /CollectionViewGradientSample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CollectionViewGradientSample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CollectionViewGradientSample.xcodeproj/project.xcworkspace/xcuserdata/payal.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pgpt10/CollectionViewGradientSample/37c50770555e224364b0b34f8be6224eb4f55bd8/CollectionViewGradientSample.xcodeproj/project.xcworkspace/xcuserdata/payal.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /CollectionViewGradientSample.xcodeproj/xcuserdata/payal.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | CollectionViewGradientSample.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /CollectionViewGradientSample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // CollectionViewGradientSample 4 | // 5 | // Created by Payal Gupta on 13/05/18. 6 | // Copyright © 2018 Payal Gupta. 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 | -------------------------------------------------------------------------------- /CollectionViewGradientSample/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 | } -------------------------------------------------------------------------------- /CollectionViewGradientSample/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /CollectionViewGradientSample/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 | -------------------------------------------------------------------------------- /CollectionViewGradientSample/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 | 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 | -------------------------------------------------------------------------------- /CollectionViewGradientSample/CustomCollectionViewCell.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CustomCollectionViewCell.swift 3 | // CollectionViewGradientSample 4 | // 5 | // Created by Payal Gupta on 13/05/18. 6 | // Copyright © 2018 Payal Gupta. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class CustomCollectionViewCell: UICollectionViewCell 12 | { 13 | //MARK: Outlets 14 | @IBOutlet weak var customLabel: UILabel! 15 | 16 | //MARK: Private Methods 17 | private lazy var gradient: CAGradientLayer = { 18 | let gradientLayer = CAGradientLayer() 19 | gradientLayer.colors = [UIColor.darkGray.cgColor, UIColor.orange.cgColor] 20 | gradientLayer.startPoint = CGPoint(x: 0, y: 0) 21 | gradientLayer.endPoint = CGPoint(x: 1, y: 1) 22 | gradientLayer.frame = self.bounds 23 | return gradientLayer 24 | }() 25 | 26 | //MARK: View Lifecycle Methods 27 | override func awakeFromNib() 28 | { 29 | super.awakeFromNib() 30 | self.customLabel.text = nil 31 | } 32 | 33 | override var isSelected: Bool{ 34 | didSet{ 35 | if self.isSelected 36 | { 37 | self.layer.insertSublayer(self.gradient, at: 0) 38 | } 39 | else 40 | { 41 | self.gradient.removeFromSuperlayer() 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /CollectionViewGradientSample/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 | -------------------------------------------------------------------------------- /CollectionViewGradientSample/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // CollectionViewGradientSample 4 | // 5 | // Created by Payal Gupta on 13/05/18. 6 | // Copyright © 2018 Payal Gupta. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController 12 | { 13 | //MARK: View Lifecycle Methods 14 | override func viewDidLoad() 15 | { 16 | super.viewDidLoad() 17 | } 18 | } 19 | 20 | // MARK: - UICollectionViewDataSource, UICollectionViewDelegate Methods 21 | extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate 22 | { 23 | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 24 | { 25 | return 12 26 | } 27 | 28 | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 29 | { 30 | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell 31 | cell.customLabel.text = "This is Index - \(indexPath.row)" 32 | return cell 33 | } 34 | 35 | func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) 36 | { 37 | //Select 1st cell when the collection view is first loaded 38 | if collectionView.indexPathsForSelectedItems?.first == nil && indexPath.row == 0 39 | { 40 | cell.isSelected = true 41 | collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionViewScrollPosition(rawValue: 0)) 42 | } 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Selectively Add & Remove Gradient — UICollectionViewCell 2 | 3 | Playing with Collection Views is so much fun and challenging. One of the things I came across is using gradients with collection views. 4 | 5 | Adding gradient to a `UICollectionView` or `UICollectionViewCell` is as simple as adding it to any other view. We had a detailed discussion over it in the previous article. 6 | 7 | Another challenge is, what if we need to add gradient to a `UICollectionViewCell` on selection and remove it when the `UICollectionViewCell` is de-selected. 8 | 9 | ## Preview 10 | 11 | 12 | ## Pre-requisites 13 | Before we start coding, let’s see what we already have 🧐 and how can we use that to solve our problem statement. 14 | 15 | Adding gradient to a view — In this article we had a detailed discussion on how to get that working. 16 | 17 | Changing the `UICollectionViewCell’s` properties based on its selection stateHere we have already discussed how isSelected handles that. 18 | 19 | Combining the above 2, now we’re left with “adding the gradient to cell when it is selected and removing it when it is de-selected”. We’re already halfway there, just need to put our knowledge in the right place. 🎯 20 | 21 | ## What's in the Project 22 | The project includes all the implementation details of how you can handle UICollectionViewCell selection with gradient. 23 | 24 | ## Promotions 25 | For more iOS related stuff, you can follow me here. 26 | -------------------------------------------------------------------------------- /Screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pgpt10/CollectionViewGradientSample/37c50770555e224364b0b34f8be6224eb4f55bd8/Screenshot.gif --------------------------------------------------------------------------------