├── .gitignore ├── Example ├── MKGradientViewExample.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── MKGradientViewExample │ ├── AppDelegate.swift │ ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ ├── Contents.json │ │ ├── Icon-1024.png │ │ ├── Icon-20.png │ │ ├── Icon-20@2x.png │ │ ├── Icon-20@3x.png │ │ ├── Icon-29.png │ │ ├── Icon-29@2x.png │ │ ├── Icon-29@3x.png │ │ ├── Icon-40.png │ │ ├── Icon-40@2x.png │ │ ├── Icon-40@3x.png │ │ ├── Icon-60@2x.png │ │ ├── Icon-60@3x.png │ │ ├── Icon-76.png │ │ ├── Icon-76@2x.png │ │ └── Icon-83.5@2x.png │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ └── ViewController.swift ├── LICENSE ├── MKGradientView.png ├── MKGradientView.podspec ├── MKGradientView.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcshareddata │ └── xcschemes │ └── MKGradientView.xcscheme ├── MKGradientView ├── Info.plist ├── MKGradientGenerator.swift ├── MKGradientLayer.swift ├── MKGradientView.h └── MKGradientView.swift ├── Package.swift └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | 28 | # Carthage 29 | # 30 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 31 | # Carthage/Checkouts 32 | 33 | Carthage/Build 34 | -------------------------------------------------------------------------------- /Example/MKGradientViewExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 50BAFB4B2153B96E00F73383 /* MKGradientView.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50BAFB4A2153B96000F73383 /* MKGradientView.framework */; }; 11 | 50BAFB4C2153B96E00F73383 /* MKGradientView.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 50BAFB4A2153B96000F73383 /* MKGradientView.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 12 | 50F22C501BD52857006983CF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50F22C4F1BD52857006983CF /* AppDelegate.swift */; }; 13 | 50F22C521BD52857006983CF /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50F22C511BD52857006983CF /* ViewController.swift */; }; 14 | 50F22C551BD52857006983CF /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 50F22C531BD52857006983CF /* Main.storyboard */; }; 15 | 50F22C571BD52857006983CF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 50F22C561BD52857006983CF /* Assets.xcassets */; }; 16 | 50F22C5A1BD52857006983CF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 50F22C581BD52857006983CF /* LaunchScreen.storyboard */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXContainerItemProxy section */ 20 | 50BAFB492153B96000F73383 /* PBXContainerItemProxy */ = { 21 | isa = PBXContainerItemProxy; 22 | containerPortal = 50BAFB452153B96000F73383 /* MKGradientView.xcodeproj */; 23 | proxyType = 2; 24 | remoteGlobalIDString = 50BAFB352153B8CD00F73383; 25 | remoteInfo = MKGradientView; 26 | }; 27 | 50BAFB4D2153B96E00F73383 /* PBXContainerItemProxy */ = { 28 | isa = PBXContainerItemProxy; 29 | containerPortal = 50BAFB452153B96000F73383 /* MKGradientView.xcodeproj */; 30 | proxyType = 1; 31 | remoteGlobalIDString = 50BAFB342153B8CD00F73383; 32 | remoteInfo = MKGradientView; 33 | }; 34 | /* End PBXContainerItemProxy section */ 35 | 36 | /* Begin PBXCopyFilesBuildPhase section */ 37 | 50BAFB4F2153B96E00F73383 /* Embed Frameworks */ = { 38 | isa = PBXCopyFilesBuildPhase; 39 | buildActionMask = 2147483647; 40 | dstPath = ""; 41 | dstSubfolderSpec = 10; 42 | files = ( 43 | 50BAFB4C2153B96E00F73383 /* MKGradientView.framework in Embed Frameworks */, 44 | ); 45 | name = "Embed Frameworks"; 46 | runOnlyForDeploymentPostprocessing = 0; 47 | }; 48 | /* End PBXCopyFilesBuildPhase section */ 49 | 50 | /* Begin PBXFileReference section */ 51 | 50BAFB452153B96000F73383 /* MKGradientView.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = MKGradientView.xcodeproj; path = ../MKGradientView.xcodeproj; sourceTree = ""; }; 52 | 50F22C4C1BD52857006983CF /* MKGradientViewExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MKGradientViewExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 53 | 50F22C4F1BD52857006983CF /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 54 | 50F22C511BD52857006983CF /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 55 | 50F22C541BD52857006983CF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 56 | 50F22C561BD52857006983CF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 57 | 50F22C591BD52857006983CF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 58 | 50F22C5B1BD52857006983CF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 59 | /* End PBXFileReference section */ 60 | 61 | /* Begin PBXFrameworksBuildPhase section */ 62 | 50F22C491BD52857006983CF /* Frameworks */ = { 63 | isa = PBXFrameworksBuildPhase; 64 | buildActionMask = 2147483647; 65 | files = ( 66 | 50BAFB4B2153B96E00F73383 /* MKGradientView.framework in Frameworks */, 67 | ); 68 | runOnlyForDeploymentPostprocessing = 0; 69 | }; 70 | /* End PBXFrameworksBuildPhase section */ 71 | 72 | /* Begin PBXGroup section */ 73 | 50BAFB462153B96000F73383 /* Products */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 50BAFB4A2153B96000F73383 /* MKGradientView.framework */, 77 | ); 78 | name = Products; 79 | sourceTree = ""; 80 | }; 81 | 50F22C431BD52857006983CF = { 82 | isa = PBXGroup; 83 | children = ( 84 | 50BAFB452153B96000F73383 /* MKGradientView.xcodeproj */, 85 | 50F22C4E1BD52857006983CF /* MKGradientViewExample */, 86 | 50F22C4D1BD52857006983CF /* Products */, 87 | ); 88 | sourceTree = ""; 89 | }; 90 | 50F22C4D1BD52857006983CF /* Products */ = { 91 | isa = PBXGroup; 92 | children = ( 93 | 50F22C4C1BD52857006983CF /* MKGradientViewExample.app */, 94 | ); 95 | name = Products; 96 | sourceTree = ""; 97 | }; 98 | 50F22C4E1BD52857006983CF /* MKGradientViewExample */ = { 99 | isa = PBXGroup; 100 | children = ( 101 | 50F22C4F1BD52857006983CF /* AppDelegate.swift */, 102 | 50F22C511BD52857006983CF /* ViewController.swift */, 103 | 50F22C561BD52857006983CF /* Assets.xcassets */, 104 | 50F22C531BD52857006983CF /* Main.storyboard */, 105 | 50F22C581BD52857006983CF /* LaunchScreen.storyboard */, 106 | 50F22C5B1BD52857006983CF /* Info.plist */, 107 | ); 108 | path = MKGradientViewExample; 109 | sourceTree = ""; 110 | }; 111 | /* End PBXGroup section */ 112 | 113 | /* Begin PBXNativeTarget section */ 114 | 50F22C4B1BD52857006983CF /* MKGradientViewExample */ = { 115 | isa = PBXNativeTarget; 116 | buildConfigurationList = 50F22C5E1BD52857006983CF /* Build configuration list for PBXNativeTarget "MKGradientViewExample" */; 117 | buildPhases = ( 118 | 50F22C481BD52857006983CF /* Sources */, 119 | 50F22C491BD52857006983CF /* Frameworks */, 120 | 50F22C4A1BD52857006983CF /* Resources */, 121 | 50BAFB4F2153B96E00F73383 /* Embed Frameworks */, 122 | ); 123 | buildRules = ( 124 | ); 125 | dependencies = ( 126 | 50BAFB4E2153B96E00F73383 /* PBXTargetDependency */, 127 | ); 128 | name = MKGradientViewExample; 129 | productName = MKGradientViewExample; 130 | productReference = 50F22C4C1BD52857006983CF /* MKGradientViewExample.app */; 131 | productType = "com.apple.product-type.application"; 132 | }; 133 | /* End PBXNativeTarget section */ 134 | 135 | /* Begin PBXProject section */ 136 | 50F22C441BD52857006983CF /* Project object */ = { 137 | isa = PBXProject; 138 | attributes = { 139 | LastUpgradeCheck = 0900; 140 | ORGANIZATIONNAME = "Max Konovalov"; 141 | TargetAttributes = { 142 | 50F22C4B1BD52857006983CF = { 143 | CreatedOnToolsVersion = 7.0.1; 144 | LastSwiftMigration = 1020; 145 | ProvisioningStyle = Manual; 146 | }; 147 | }; 148 | }; 149 | buildConfigurationList = 50F22C471BD52857006983CF /* Build configuration list for PBXProject "MKGradientViewExample" */; 150 | compatibilityVersion = "Xcode 3.2"; 151 | developmentRegion = English; 152 | hasScannedForEncodings = 0; 153 | knownRegions = ( 154 | English, 155 | en, 156 | Base, 157 | ); 158 | mainGroup = 50F22C431BD52857006983CF; 159 | productRefGroup = 50F22C4D1BD52857006983CF /* Products */; 160 | projectDirPath = ""; 161 | projectReferences = ( 162 | { 163 | ProductGroup = 50BAFB462153B96000F73383 /* Products */; 164 | ProjectRef = 50BAFB452153B96000F73383 /* MKGradientView.xcodeproj */; 165 | }, 166 | ); 167 | projectRoot = ""; 168 | targets = ( 169 | 50F22C4B1BD52857006983CF /* MKGradientViewExample */, 170 | ); 171 | }; 172 | /* End PBXProject section */ 173 | 174 | /* Begin PBXReferenceProxy section */ 175 | 50BAFB4A2153B96000F73383 /* MKGradientView.framework */ = { 176 | isa = PBXReferenceProxy; 177 | fileType = wrapper.framework; 178 | path = MKGradientView.framework; 179 | remoteRef = 50BAFB492153B96000F73383 /* PBXContainerItemProxy */; 180 | sourceTree = BUILT_PRODUCTS_DIR; 181 | }; 182 | /* End PBXReferenceProxy section */ 183 | 184 | /* Begin PBXResourcesBuildPhase section */ 185 | 50F22C4A1BD52857006983CF /* Resources */ = { 186 | isa = PBXResourcesBuildPhase; 187 | buildActionMask = 2147483647; 188 | files = ( 189 | 50F22C5A1BD52857006983CF /* LaunchScreen.storyboard in Resources */, 190 | 50F22C571BD52857006983CF /* Assets.xcassets in Resources */, 191 | 50F22C551BD52857006983CF /* Main.storyboard in Resources */, 192 | ); 193 | runOnlyForDeploymentPostprocessing = 0; 194 | }; 195 | /* End PBXResourcesBuildPhase section */ 196 | 197 | /* Begin PBXSourcesBuildPhase section */ 198 | 50F22C481BD52857006983CF /* Sources */ = { 199 | isa = PBXSourcesBuildPhase; 200 | buildActionMask = 2147483647; 201 | files = ( 202 | 50F22C521BD52857006983CF /* ViewController.swift in Sources */, 203 | 50F22C501BD52857006983CF /* AppDelegate.swift in Sources */, 204 | ); 205 | runOnlyForDeploymentPostprocessing = 0; 206 | }; 207 | /* End PBXSourcesBuildPhase section */ 208 | 209 | /* Begin PBXTargetDependency section */ 210 | 50BAFB4E2153B96E00F73383 /* PBXTargetDependency */ = { 211 | isa = PBXTargetDependency; 212 | name = MKGradientView; 213 | targetProxy = 50BAFB4D2153B96E00F73383 /* PBXContainerItemProxy */; 214 | }; 215 | /* End PBXTargetDependency section */ 216 | 217 | /* Begin PBXVariantGroup section */ 218 | 50F22C531BD52857006983CF /* Main.storyboard */ = { 219 | isa = PBXVariantGroup; 220 | children = ( 221 | 50F22C541BD52857006983CF /* Base */, 222 | ); 223 | name = Main.storyboard; 224 | sourceTree = ""; 225 | }; 226 | 50F22C581BD52857006983CF /* LaunchScreen.storyboard */ = { 227 | isa = PBXVariantGroup; 228 | children = ( 229 | 50F22C591BD52857006983CF /* Base */, 230 | ); 231 | name = LaunchScreen.storyboard; 232 | sourceTree = ""; 233 | }; 234 | /* End PBXVariantGroup section */ 235 | 236 | /* Begin XCBuildConfiguration section */ 237 | 50F22C5C1BD52857006983CF /* Debug */ = { 238 | isa = XCBuildConfiguration; 239 | buildSettings = { 240 | ALWAYS_SEARCH_USER_PATHS = NO; 241 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 242 | CLANG_CXX_LIBRARY = "libc++"; 243 | CLANG_ENABLE_MODULES = YES; 244 | CLANG_ENABLE_OBJC_ARC = YES; 245 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 246 | CLANG_WARN_BOOL_CONVERSION = YES; 247 | CLANG_WARN_COMMA = YES; 248 | CLANG_WARN_CONSTANT_CONVERSION = YES; 249 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 250 | CLANG_WARN_EMPTY_BODY = YES; 251 | CLANG_WARN_ENUM_CONVERSION = YES; 252 | CLANG_WARN_INFINITE_RECURSION = YES; 253 | CLANG_WARN_INT_CONVERSION = YES; 254 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 255 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 256 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 257 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 258 | CLANG_WARN_STRICT_PROTOTYPES = YES; 259 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 260 | CLANG_WARN_UNREACHABLE_CODE = YES; 261 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 262 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 263 | COPY_PHASE_STRIP = NO; 264 | DEBUG_INFORMATION_FORMAT = dwarf; 265 | ENABLE_STRICT_OBJC_MSGSEND = YES; 266 | ENABLE_TESTABILITY = YES; 267 | GCC_C_LANGUAGE_STANDARD = gnu99; 268 | GCC_DYNAMIC_NO_PIC = NO; 269 | GCC_NO_COMMON_BLOCKS = YES; 270 | GCC_OPTIMIZATION_LEVEL = 0; 271 | GCC_PREPROCESSOR_DEFINITIONS = ( 272 | "DEBUG=1", 273 | "$(inherited)", 274 | ); 275 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 276 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 277 | GCC_WARN_UNDECLARED_SELECTOR = YES; 278 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 279 | GCC_WARN_UNUSED_FUNCTION = YES; 280 | GCC_WARN_UNUSED_VARIABLE = YES; 281 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 282 | MTL_ENABLE_DEBUG_INFO = YES; 283 | ONLY_ACTIVE_ARCH = YES; 284 | SDKROOT = iphoneos; 285 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 286 | TARGETED_DEVICE_FAMILY = "1,2"; 287 | }; 288 | name = Debug; 289 | }; 290 | 50F22C5D1BD52857006983CF /* Release */ = { 291 | isa = XCBuildConfiguration; 292 | buildSettings = { 293 | ALWAYS_SEARCH_USER_PATHS = NO; 294 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 295 | CLANG_CXX_LIBRARY = "libc++"; 296 | CLANG_ENABLE_MODULES = YES; 297 | CLANG_ENABLE_OBJC_ARC = YES; 298 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 299 | CLANG_WARN_BOOL_CONVERSION = YES; 300 | CLANG_WARN_COMMA = YES; 301 | CLANG_WARN_CONSTANT_CONVERSION = YES; 302 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 303 | CLANG_WARN_EMPTY_BODY = YES; 304 | CLANG_WARN_ENUM_CONVERSION = YES; 305 | CLANG_WARN_INFINITE_RECURSION = YES; 306 | CLANG_WARN_INT_CONVERSION = YES; 307 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 308 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 309 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 310 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 311 | CLANG_WARN_STRICT_PROTOTYPES = YES; 312 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 313 | CLANG_WARN_UNREACHABLE_CODE = YES; 314 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 315 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 316 | COPY_PHASE_STRIP = NO; 317 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 318 | ENABLE_NS_ASSERTIONS = NO; 319 | ENABLE_STRICT_OBJC_MSGSEND = YES; 320 | GCC_C_LANGUAGE_STANDARD = gnu99; 321 | GCC_NO_COMMON_BLOCKS = YES; 322 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 323 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 324 | GCC_WARN_UNDECLARED_SELECTOR = YES; 325 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 326 | GCC_WARN_UNUSED_FUNCTION = YES; 327 | GCC_WARN_UNUSED_VARIABLE = YES; 328 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 329 | MTL_ENABLE_DEBUG_INFO = NO; 330 | SDKROOT = iphoneos; 331 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 332 | TARGETED_DEVICE_FAMILY = "1,2"; 333 | VALIDATE_PRODUCT = YES; 334 | }; 335 | name = Release; 336 | }; 337 | 50F22C5F1BD52857006983CF /* Debug */ = { 338 | isa = XCBuildConfiguration; 339 | buildSettings = { 340 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 341 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 342 | CODE_SIGN_STYLE = Manual; 343 | DEVELOPMENT_TEAM = ""; 344 | INFOPLIST_FILE = MKGradientViewExample/Info.plist; 345 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 346 | PRODUCT_BUNDLE_IDENTIFIER = com.maxkonovalov.MKGradientViewExample; 347 | PRODUCT_NAME = "$(TARGET_NAME)"; 348 | PROVISIONING_PROFILE_SPECIFIER = ""; 349 | SWIFT_VERSION = 5.0; 350 | }; 351 | name = Debug; 352 | }; 353 | 50F22C601BD52857006983CF /* Release */ = { 354 | isa = XCBuildConfiguration; 355 | buildSettings = { 356 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 357 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 358 | CODE_SIGN_STYLE = Manual; 359 | DEVELOPMENT_TEAM = ""; 360 | INFOPLIST_FILE = MKGradientViewExample/Info.plist; 361 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 362 | PRODUCT_BUNDLE_IDENTIFIER = com.maxkonovalov.MKGradientViewExample; 363 | PRODUCT_NAME = "$(TARGET_NAME)"; 364 | PROVISIONING_PROFILE_SPECIFIER = ""; 365 | SWIFT_VERSION = 5.0; 366 | }; 367 | name = Release; 368 | }; 369 | /* End XCBuildConfiguration section */ 370 | 371 | /* Begin XCConfigurationList section */ 372 | 50F22C471BD52857006983CF /* Build configuration list for PBXProject "MKGradientViewExample" */ = { 373 | isa = XCConfigurationList; 374 | buildConfigurations = ( 375 | 50F22C5C1BD52857006983CF /* Debug */, 376 | 50F22C5D1BD52857006983CF /* Release */, 377 | ); 378 | defaultConfigurationIsVisible = 0; 379 | defaultConfigurationName = Release; 380 | }; 381 | 50F22C5E1BD52857006983CF /* Build configuration list for PBXNativeTarget "MKGradientViewExample" */ = { 382 | isa = XCConfigurationList; 383 | buildConfigurations = ( 384 | 50F22C5F1BD52857006983CF /* Debug */, 385 | 50F22C601BD52857006983CF /* Release */, 386 | ); 387 | defaultConfigurationIsVisible = 0; 388 | defaultConfigurationName = Release; 389 | }; 390 | /* End XCConfigurationList section */ 391 | }; 392 | rootObject = 50F22C441BD52857006983CF /* Project object */; 393 | } 394 | -------------------------------------------------------------------------------- /Example/MKGradientViewExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Example/MKGradientViewExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Example/MKGradientViewExample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // MKGradientViewExample 4 | // 5 | // Created by Max Konovalov on 19/10/15. 6 | // Copyright © 2015 Max Konovalov. 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]? = nil) -> Bool { 18 | return true 19 | } 20 | 21 | func applicationWillResignActive(_ application: UIApplication) { 22 | // 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. 23 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 24 | } 25 | 26 | func applicationDidEnterBackground(_ application: UIApplication) { 27 | // 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. 28 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 29 | } 30 | 31 | func applicationWillEnterForeground(_ application: UIApplication) { 32 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 33 | } 34 | 35 | func applicationDidBecomeActive(_ application: UIApplication) { 36 | // 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. 37 | } 38 | 39 | func applicationWillTerminate(_ application: UIApplication) { 40 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 41 | } 42 | 43 | 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "20x20", 5 | "idiom": "iphone", 6 | "filename" : "Icon-20@2x.png", 7 | "scale": "2x" 8 | }, 9 | { 10 | "size" : "20x20", 11 | "idiom": "iphone", 12 | "filename" : "Icon-20@3x.png", 13 | "scale": "3x" 14 | }, 15 | { 16 | "size" : "20x20", 17 | "idiom": "ipad", 18 | "filename" : "Icon-20.png", 19 | "scale": "1x" 20 | }, 21 | { 22 | "size" : "20x20", 23 | "idiom": "ipad", 24 | "filename" : "Icon-20@2x.png", 25 | "scale": "2x" 26 | }, 27 | { 28 | "size" : "29x29", 29 | "idiom" : "iphone", 30 | "filename" : "Icon-29@2x.png", 31 | "scale" : "2x" 32 | }, 33 | { 34 | "size" : "29x29", 35 | "idiom" : "iphone", 36 | "filename" : "Icon-29@3x.png", 37 | "scale" : "3x" 38 | }, 39 | { 40 | "size" : "40x40", 41 | "idiom" : "iphone", 42 | "filename" : "Icon-40@2x.png", 43 | "scale" : "2x" 44 | }, 45 | { 46 | "size" : "40x40", 47 | "idiom" : "iphone", 48 | "filename" : "Icon-40@3x.png", 49 | "scale" : "3x" 50 | }, 51 | { 52 | "size" : "60x60", 53 | "idiom" : "iphone", 54 | "filename" : "Icon-60@2x.png", 55 | "scale" : "2x" 56 | }, 57 | { 58 | "size" : "60x60", 59 | "idiom" : "iphone", 60 | "filename" : "Icon-60@3x.png", 61 | "scale" : "3x" 62 | }, 63 | { 64 | "size" : "29x29", 65 | "idiom" : "ipad", 66 | "filename" : "Icon-29.png", 67 | "scale" : "1x" 68 | }, 69 | { 70 | "size" : "29x29", 71 | "idiom" : "ipad", 72 | "filename" : "Icon-29@2x.png", 73 | "scale" : "2x" 74 | }, 75 | { 76 | "size" : "40x40", 77 | "idiom" : "ipad", 78 | "filename" : "Icon-40.png", 79 | "scale" : "1x" 80 | }, 81 | { 82 | "size" : "40x40", 83 | "idiom" : "ipad", 84 | "filename" : "Icon-40@2x.png", 85 | "scale" : "2x" 86 | }, 87 | { 88 | "size" : "76x76", 89 | "idiom" : "ipad", 90 | "filename" : "Icon-76.png", 91 | "scale" : "1x" 92 | }, 93 | { 94 | "size" : "76x76", 95 | "idiom" : "ipad", 96 | "filename" : "Icon-76@2x.png", 97 | "scale" : "2x" 98 | }, 99 | { 100 | "size" : "83.5x83.5", 101 | "idiom" : "ipad", 102 | "filename" : "Icon-83.5@2x.png", 103 | "scale" : "2x" 104 | }, 105 | { 106 | "size" : "1024x1024", 107 | "idiom" : "ios-marketing", 108 | "filename" : "Icon-1024.png", 109 | "scale" : "1x" 110 | } 111 | ], 112 | "info" : { 113 | "version" : 1, 114 | "author" : "xcode" 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-1024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-1024.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-20.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-20@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-20@2x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-20@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-20@3x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-29.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-29.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-29@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-29@2x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-29@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-29@3x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-40.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-40@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-40@2x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-40@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-40@3x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-60@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-60@2x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-60@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-60@3x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-76.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-76@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-76@2x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-83.5@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxkonovalov/MKGradientView/a92da19ee20701b111a25312de4e90b1d068c3e2/Example/MKGradientViewExample/Assets.xcassets/AppIcon.appiconset/Icon-83.5@2x.png -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /Example/MKGradientViewExample/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 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Example/MKGradientViewExample/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 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /Example/MKGradientViewExample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | Gradient 9 | CFBundleExecutable 10 | $(EXECUTABLE_NAME) 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSRequiresIPhoneOS 26 | 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIMainStoryboardFile 30 | Main 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UIStatusBarHidden 36 | 37 | UISupportedInterfaceOrientations 38 | 39 | UIInterfaceOrientationPortrait 40 | 41 | UISupportedInterfaceOrientations~ipad 42 | 43 | UIInterfaceOrientationPortrait 44 | UIInterfaceOrientationPortraitUpsideDown 45 | UIInterfaceOrientationLandscapeLeft 46 | UIInterfaceOrientationLandscapeRight 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Example/MKGradientViewExample/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // MKGradientViewExample 4 | // 5 | // Created by Max Konovalov on 19/10/15. 6 | // Copyright © 2015 Max Konovalov. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import MKGradientView 11 | 12 | class ViewController: UIViewController { 13 | 14 | @IBOutlet weak var linearGradientView: GradientView! 15 | @IBOutlet weak var radialGradientView: GradientView! 16 | @IBOutlet weak var conicalGradientView: GradientView! 17 | @IBOutlet weak var bilinearGradientView: GradientView! 18 | 19 | 20 | override func viewDidLoad() { 21 | super.viewDidLoad() 22 | 23 | var colors = [UIColor]() 24 | let n = 9 25 | for i in 0.. 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MKGradientView.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MKGradientView.xcodeproj/xcshareddata/xcschemes/MKGradientView.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 34 | 35 | 45 | 46 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 70 | 71 | 72 | 73 | 75 | 76 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /MKGradientView/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 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | 22 | 23 | -------------------------------------------------------------------------------- /MKGradientView/MKGradientGenerator.swift: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Max Konovalov 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | import UIKit 26 | 27 | // MARK: - Gradient Generator 28 | 29 | @objc(MKGradientType) 30 | public enum GradientType: Int { 31 | case linear 32 | case radial 33 | case conical 34 | case bilinear 35 | } 36 | 37 | internal final class GradientGenerator { 38 | class func gradientImage( 39 | type: GradientType, 40 | size: CGSize, 41 | colors: [CGColor], 42 | colors2: [CGColor], 43 | locations: [Float]?, 44 | locations2: [Float]?, 45 | startPoint: CGPoint?, 46 | endPoint: CGPoint?, 47 | startPoint2: CGPoint?, 48 | endPoint2: CGPoint?, 49 | scale: CGFloat? = nil 50 | ) -> CGImage? { 51 | let w = Int(size.width * (scale ?? UIScreen.main.scale)) 52 | let h = Int(size.height * (scale ?? UIScreen.main.scale)) 53 | 54 | guard w > 0, h > 0 else { 55 | return nil 56 | } 57 | 58 | let bitsPerComponent: Int = MemoryLayout.size * 8 59 | let bytesPerPixel: Int = bitsPerComponent * 4 / 8 60 | 61 | let colorSpace = CGColorSpaceCreateDeviceRGB() 62 | let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) 63 | 64 | var data = [RGBA]() 65 | 66 | for y in 0 ..< h { 67 | for x in 0 ..< w { 68 | let c = pixelDataForGradient( 69 | type, 70 | colors: [colors, colors2], 71 | locations: [locations, locations2], 72 | startPoints: [startPoint, startPoint2], 73 | endPoints: [endPoint, endPoint2], 74 | point: CGPoint(x: x, y: y), 75 | size: CGSize(width: w, height: h) 76 | ) 77 | data.append(c) 78 | } 79 | } 80 | 81 | // Fix for #8 - force retain `data` to prevent crash when CGContext uses the buffer 82 | let image: CGImage? = withExtendedLifetime(&data) { (data: UnsafeMutableRawPointer) -> CGImage? in 83 | guard let ctx = CGContext( 84 | data: data, 85 | width: w, 86 | height: h, 87 | bitsPerComponent: bitsPerComponent, 88 | bytesPerRow: w * bytesPerPixel, 89 | space: colorSpace, 90 | bitmapInfo: bitmapInfo.rawValue 91 | ) else { 92 | return nil 93 | } 94 | 95 | return ctx.makeImage() 96 | } 97 | 98 | return image 99 | } 100 | 101 | private class func pixelDataForGradient( 102 | _ gradientType: GradientType, 103 | colors: [[CGColor]], 104 | locations: [[Float]?], 105 | startPoints: [CGPoint?], 106 | endPoints: [CGPoint?], 107 | point: CGPoint, 108 | size: CGSize 109 | ) -> RGBA { 110 | assert(!colors.isEmpty) 111 | 112 | var colors = colors 113 | var locations = locations 114 | var startPoints = startPoints 115 | var endPoints = endPoints 116 | 117 | if gradientType == .bilinear, colors.count == 1 { 118 | colors.append([UIColor.clear.cgColor]) 119 | } 120 | 121 | for (index, colorArray) in colors.enumerated() { 122 | if gradientType != .bilinear, index > 0 { 123 | continue 124 | } 125 | if locations.count <= index { 126 | locations.append(uniformLocationsWithCount(colorArray.count)) 127 | } else if locations[index] == nil { 128 | locations[index] = uniformLocationsWithCount(colorArray.count) 129 | } 130 | if startPoints.count <= index { 131 | startPoints.append(nil) 132 | } 133 | if endPoints.count <= index { 134 | endPoints.append(nil) 135 | } 136 | } 137 | 138 | switch gradientType { 139 | case .linear: 140 | let g0 = startPoints[0] ?? CGPoint(x: 0.5, y: 0.0) 141 | let g1 = endPoints[0] ?? CGPoint(x: 0.5, y: 1.0) 142 | let t = linearGradientStop(point, size, g0, g1) 143 | return interpolatedColor(t, colors[0], locations[0]!) 144 | case .radial: 145 | let g0 = startPoints[0] ?? CGPoint(x: 0.5, y: 0.5) 146 | let g1 = endPoints[0] ?? CGPoint(x: 1.0, y: 0.5) 147 | let t = radialGradientStop(point, size, g0, g1) 148 | return interpolatedColor(t, colors[0], locations[0]!) 149 | case .conical: 150 | let g0 = startPoints[0] ?? CGPoint(x: 0.5, y: 0.5) 151 | let g1 = endPoints[0] ?? CGPoint(x: 1.0, y: 0.5) 152 | let t = conicalGradientStop(point, size, g0, g1) 153 | return interpolatedColor(t, colors[0], locations[0]!) 154 | case .bilinear: 155 | let g0x = startPoints[0] ?? CGPoint(x: 0.0, y: 0.5) 156 | let g1x = endPoints[0] ?? CGPoint(x: 1.0, y: 0.5) 157 | let tx = linearGradientStop(point, size, g0x, g1x) 158 | let g0y = startPoints[1] ?? CGPoint(x: 0.5, y: 0.0) 159 | let g1y = endPoints[1] ?? CGPoint(x: 0.5, y: 1.0) 160 | let ty = linearGradientStop(point, size, g0y, g1y) 161 | let c1 = interpolatedColor(tx, colors[0], locations[0]!) 162 | let c2 = interpolatedColor(tx, colors[1], locations[1]!) 163 | let c = interpolatedColor(ty, [c1.cgColor, c2.cgColor], [0.0, 1.0]) 164 | return c 165 | } 166 | } 167 | 168 | private class func uniformLocationsWithCount(_ count: Int) -> [Float] { 169 | var locations = [Float]() 170 | for i in 0 ..< count { 171 | locations.append(Float(i) / Float(count - 1)) 172 | } 173 | return locations 174 | } 175 | 176 | private class func linearGradientStop(_ point: CGPoint, _ size: CGSize, _ g0: CGPoint, _ g1: CGPoint) -> Float { 177 | let s = CGPoint(x: size.width * (g1.x - g0.x), y: size.height * (g1.y - g0.y)) 178 | let p = CGPoint(x: point.x - size.width * g0.x, y: point.y - size.height * g0.y) 179 | let t = (p.x * s.x + p.y * s.y) / (s.x * s.x + s.y * s.y) 180 | return Float(t) 181 | } 182 | 183 | private class func radialGradientStop(_ point: CGPoint, _ size: CGSize, _ g0: CGPoint, _ g1: CGPoint) -> Float { 184 | let c = CGPoint(x: size.width * g0.x, y: size.height * g0.y) 185 | let s = CGPoint(x: size.width * (g1.x - g0.x), y: size.height * (g1.y - g0.y)) 186 | let d = sqrt(s.x * s.x + s.y * s.y) 187 | let p = CGPoint(x: point.x - c.x, y: point.y - c.y) 188 | let r = sqrt(p.x * p.x + p.y * p.y) 189 | let t = r / d 190 | return Float(t) 191 | } 192 | 193 | private class func conicalGradientStop(_ point: CGPoint, _ size: CGSize, _ g0: CGPoint, _ g1: CGPoint) -> Float { 194 | let c = CGPoint(x: size.width * g0.x, y: size.height * g0.y) 195 | let s = CGPoint(x: size.width * (g1.x - g0.x), y: size.height * (g1.y - g0.y)) 196 | let q = atan2(s.y, s.x) 197 | let p = CGPoint(x: point.x - c.x, y: point.y - c.y) 198 | var a = atan2(p.y, p.x) - q 199 | if a < 0 { 200 | a += 2 * .pi 201 | } 202 | let t = a / (2 * .pi) 203 | return Float(t) 204 | } 205 | 206 | private class func interpolatedColor(_ t: Float, _ colors: [CGColor], _ locations: [Float]) -> RGBA { 207 | assert(!colors.isEmpty) 208 | assert(colors.count == locations.count) 209 | 210 | var p0: Float = 0 211 | var p1: Float = 1 212 | 213 | var c0 = colors.first! 214 | var c1 = colors.last! 215 | 216 | for (i, v) in locations.enumerated() { 217 | if v > p0, t >= v { 218 | p0 = v 219 | c0 = colors[i] 220 | } 221 | if v < p1, t <= v { 222 | p1 = v 223 | c1 = colors[i] 224 | } 225 | } 226 | 227 | let p: Float 228 | if p0 == p1 { 229 | p = 0 230 | } else { 231 | p = lerp(t, inRange: p0 ... p1, outRange: 0 ... 1) 232 | } 233 | 234 | let color0 = RGBA(c0) 235 | let color1 = RGBA(c1) 236 | 237 | return color0.interpolate(to: color1, p) 238 | } 239 | } 240 | 241 | // MARK: - Color Data 242 | 243 | private struct RGBA: Equatable { 244 | var r: UInt8 245 | var g: UInt8 246 | var b: UInt8 247 | var a: UInt8 248 | } 249 | 250 | private extension RGBA { 251 | init() { 252 | self.init(r: 0, g: 0, b: 0, a: 0) 253 | } 254 | 255 | init(_ hex: Int) { 256 | let r = UInt8((hex >> 16) & 0xFF) 257 | let g = UInt8((hex >> 08) & 0xFF) 258 | let b = UInt8((hex >> 00) & 0xFF) 259 | self.init(r: r, g: g, b: b, a: 0xFF) 260 | } 261 | 262 | init(_ color: UIColor) { 263 | var r: CGFloat = 0 264 | var g: CGFloat = 0 265 | var b: CGFloat = 0 266 | var a: CGFloat = 0 267 | color.getRed(&r, green: &g, blue: &b, alpha: &a) 268 | self.init(r: UInt8(r * 0xFF), g: UInt8(g * 0xFF), b: UInt8(b * 0xFF), a: UInt8(a * 0xFF)) 269 | } 270 | 271 | init(_ color: CGColor) { 272 | let c = color.components?.map { min(max($0, 0.0), 1.0) } ?? [] 273 | switch c.count { 274 | case 2: 275 | self.init(r: UInt8(c[0] * 0xFF), g: UInt8(c[0] * 0xFF), b: UInt8(c[0] * 0xFF), a: UInt8(c[1] * 0xFF)) 276 | case 4: 277 | self.init(r: UInt8(c[0] * 0xFF), g: UInt8(c[1] * 0xFF), b: UInt8(c[2] * 0xFF), a: UInt8(c[3] * 0xFF)) 278 | default: 279 | self.init() 280 | } 281 | } 282 | 283 | var uiColor: UIColor { 284 | return UIColor(red: CGFloat(r) / 0xFF, green: CGFloat(g) / 0xFF, blue: CGFloat(b) / 0xFF, alpha: CGFloat(a) / 0xFF) 285 | } 286 | 287 | var cgColor: CGColor { 288 | return uiColor.cgColor 289 | } 290 | 291 | func interpolate(to color: RGBA, _ t: Float) -> RGBA { 292 | let r = lerp(t, self.r, color.r) 293 | let g = lerp(t, self.g, color.g) 294 | let b = lerp(t, self.b, color.b) 295 | let a = lerp(t, self.a, color.a) 296 | return RGBA(r: r, g: g, b: b, a: a) 297 | } 298 | } 299 | 300 | // MARK: - Utility 301 | 302 | private func lerp(_ t: Float, _ a: UInt8, _ b: UInt8) -> UInt8 { 303 | return UInt8(Float(a) + min(max(t, 0), 1) * (Float(b) - Float(a))) 304 | } 305 | 306 | private func lerp(_ value: Float, inRange: ClosedRange, outRange: ClosedRange) -> Float { 307 | return (value - inRange.lowerBound) * (outRange.upperBound - outRange.lowerBound) / (inRange.upperBound - inRange.lowerBound) + outRange.lowerBound 308 | } 309 | 310 | // MARK: - Extensions 311 | 312 | private extension Array { 313 | subscript(safe index: Int) -> Element? { 314 | return indices ~= index ? self[index] : nil 315 | } 316 | } 317 | -------------------------------------------------------------------------------- /MKGradientView/MKGradientLayer.swift: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Max Konovalov 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | import UIKit 26 | 27 | @objc(MKGradientLayer) 28 | open class GradientLayer: CALayer { 29 | /// The array of CGColorRef objects defining the color of each gradient stop. 30 | /// For `.bilinear` gradient type defines X-direction gradient stops. 31 | open var colors: [CGColor] = [UIColor.clear.cgColor, UIColor.clear.cgColor] { 32 | didSet { 33 | setNeedsDisplay() 34 | } 35 | } 36 | 37 | /// The array of Y-direction gradient stops for `.bilinear` gradient type. 38 | /// Ignored for other gradient types. 39 | open var colors2: [CGColor] = [UIColor.clear.cgColor, UIColor.clear.cgColor] { 40 | didSet { 41 | setNeedsDisplay() 42 | } 43 | } 44 | 45 | /// An optional array of Floats defining the location of each gradient stop as a value in the range [0.0, 1.0]. The values must be monotonically increasing. If a nil array is given, the stops are assumed to spread uniformly across the [0.0, 1.0] range. The number of elements must be equal to `colors` array count. 46 | /// Defines X-direction color locations for `.bilinear` gradient type. 47 | open var locations: [Float]? { 48 | didSet { 49 | setNeedsDisplay() 50 | } 51 | } 52 | 53 | /// An optional array of Y-direction color locations for `.bilinear` gradient type. The number of elements must be equal to `colors2` array count. 54 | /// Ignored for other gradient types. 55 | open var locations2: [Float]? { 56 | didSet { 57 | setNeedsDisplay() 58 | } 59 | } 60 | 61 | /// The start point of the gradient when drawn into the layer's coordinate space. The start point corresponds to the first gradient stop. The points are defined in a unit coordinate space that is then mapped to the layer's bounds rectangle when drawn. (I.e. [0.0, 0.0] is the bottom-left corner of the layer, [1.0, 1.0] is the top-right corner.). 62 | /// 63 | /// The default values for gradient types are: 64 | /// 65 | /// - `.linear`: [0.5, 0.0] -> [0.5, 1.0] 66 | /// - `.radial`: [0.5, 0.5] -> [1.0, 0.5] 67 | /// - `.conical`: [0.5, 0.5] -> [1.0, 0.5] 68 | /// - `.bilinear` (X-direction): [0.0, 0.5] -> [1.0, 0.5] 69 | open var startPoint: CGPoint? { 70 | didSet { 71 | setNeedsDisplay() 72 | } 73 | } 74 | 75 | /// The end point of the gradient when drawn into the layer's coordinate space. The end point corresponds to the last gradient stop. The points are defined in a unit coordinate space that is then mapped to the layer's bounds rectangle when drawn. (I.e. [0.0, 0.0] is the bottom-left corner of the layer, [1.0, 1.0] is the top-right corner.). 76 | open var endPoint: CGPoint? { 77 | didSet { 78 | setNeedsDisplay() 79 | } 80 | } 81 | 82 | /// The start point of the `.bilinear` gradient's Y-direction, defaults to [0.5, 0.0] -> [0.5, 1.0]. 83 | /// Ignored for other gradient types. 84 | open var startPoint2: CGPoint? { 85 | didSet { 86 | setNeedsDisplay() 87 | } 88 | } 89 | 90 | /// The end point of the `.bilinear` gradient's Y-direction, defaults to [0.5, 0.0] -> [0.5, 1.0]. 91 | /// Ignored for other gradient types. 92 | open var endPoint2: CGPoint? { 93 | didSet { 94 | setNeedsDisplay() 95 | } 96 | } 97 | 98 | /// The kind of gradient that will be drawn. 99 | open var type: GradientType = .linear { 100 | didSet { 101 | setNeedsDisplay() 102 | } 103 | } 104 | 105 | // MARK: Content drawing 106 | 107 | open override func draw(in ctx: CGContext) { 108 | if let backgroundColor = backgroundColor { 109 | ctx.setFillColor(backgroundColor) 110 | ctx.fill(bounds) 111 | } 112 | 113 | if let img = GradientGenerator.gradientImage( 114 | type: type, 115 | size: bounds.size, 116 | colors: colors, 117 | colors2: colors2, 118 | locations: locations, 119 | locations2: locations2, 120 | startPoint: startPoint, 121 | endPoint: endPoint, 122 | startPoint2: startPoint2, 123 | endPoint2: endPoint2, 124 | scale: contentsScale 125 | ) { 126 | ctx.draw(img, in: bounds) 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /MKGradientView/MKGradientView.h: -------------------------------------------------------------------------------- 1 | // 2 | // MKGradientView.h 3 | // MKGradientView 4 | // 5 | // Created by Max Konovalov on 20/09/2018. 6 | // Copyright © 2018 Max Konovalov. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for MKGradientView. 12 | FOUNDATION_EXPORT double MKGradientViewVersionNumber; 13 | 14 | //! Project version string for MKGradientView. 15 | FOUNDATION_EXPORT const unsigned char MKGradientViewVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /MKGradientView/MKGradientView.swift: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Max Konovalov 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | import UIKit 26 | 27 | @IBDesignable 28 | @objc(MKGradientView) 29 | open class GradientView: UIView { 30 | @IBInspectable @available(*, unavailable, message: "This property is reserved for Interface Builder only.") 31 | public var gradientType: Int { 32 | set { 33 | type = GradientType(rawValue: newValue) ?? .linear 34 | } 35 | get { 36 | type.rawValue 37 | } 38 | } 39 | 40 | @IBInspectable @available(*, unavailable, message: "This property is reserved for Interface Builder only.") 41 | public var startColor: UIColor { 42 | set { 43 | if colors.isEmpty { 44 | colors.append(newValue) 45 | colors.append(UIColor.clear) 46 | } else { 47 | colors[0] = newValue 48 | } 49 | } 50 | get { 51 | return (colors.count >= 1) ? colors[0] : UIColor.clear 52 | } 53 | } 54 | 55 | @IBInspectable @available(*, unavailable, message: "This property is reserved for Interface Builder only.") 56 | public var endColor: UIColor { 57 | set { 58 | if colors.isEmpty { 59 | colors.append(UIColor.clear) 60 | colors.append(newValue) 61 | } else { 62 | colors[1] = newValue 63 | } 64 | } 65 | get { 66 | return (colors.count >= 2) ? colors[1] : UIColor.clear 67 | } 68 | } 69 | 70 | /// The array of colors defining the color of each gradient stop. 71 | /// For `.bilinear` gradient type defines X-direction gradient stops. 72 | open var colors: [UIColor] = [.clear, .clear] { 73 | didSet { 74 | gradientLayer.colors = colors.map { $0.cgColor } 75 | } 76 | } 77 | 78 | /// The array of Y-direction gradient stops for `.bilinear` gradient type. 79 | /// Ignored for other gradient types. 80 | open var colors2: [UIColor] = [.clear, .clear] { 81 | didSet { 82 | gradientLayer.colors2 = colors2.map { $0.cgColor } 83 | } 84 | } 85 | 86 | /// An optional array of Floats defining the location of each gradient stop as a value in the range [0.0, 1.0]. The values must be monotonically increasing. If a nil array is given, the stops are assumed to spread uniformly across the [0.0, 1.0] range. The number of elements must be equal to `colors` array count. 87 | /// Defines X-direction color locations for `.bilinear` gradient type. 88 | open var locations: [Float]? { 89 | didSet { 90 | gradientLayer.locations = locations 91 | } 92 | } 93 | 94 | /// An optional array of Y-direction color locations for `.bilinear` gradient type. The number of elements must be equal to `colors2` array count. 95 | /// Ignored for other gradient types. 96 | open var locations2: [Float]? { 97 | didSet { 98 | gradientLayer.locations2 = locations2 99 | } 100 | } 101 | 102 | /// The start point of the gradient when drawn into the layer's coordinate space. The start point corresponds to the first gradient stop. The points are defined in a unit coordinate space that is then mapped to the layer's bounds rectangle when drawn. (I.e. [0.0, 0.0] is the bottom-left corner of the layer, [1.0, 1.0] is the top-right corner.). 103 | /// 104 | /// The default values for gradient types are: 105 | /// 106 | /// - `.linear`: [0.5, 0.0] -> [0.5, 1.0] 107 | /// - `.radial`: [0.5, 0.5] -> [1.0, 0.5] 108 | /// - `.conical`: [0.5, 0.5] -> [1.0, 0.5] 109 | /// - `.bilinear` (X-direction): [0.0, 0.5] -> [1.0, 0.5] 110 | @IBInspectable open var startPoint: CGPoint = CGPoint(x: 0, y: 0) { 111 | didSet { 112 | gradientLayer.startPoint = startPoint 113 | } 114 | } 115 | 116 | /// The end point of the gradient when drawn into the layer's coordinate space. The end point corresponds to the last gradient stop. The points are defined in a unit coordinate space that is then mapped to the layer's bounds rectangle when drawn. (I.e. [0.0, 0.0] is the bottom-left corner of the layer, [1.0, 1.0] is the top-right corner.). 117 | @IBInspectable open var endPoint: CGPoint = CGPoint(x: 1, y: 1) { 118 | didSet { 119 | gradientLayer.endPoint = endPoint 120 | } 121 | } 122 | 123 | /// The start point of the `.bilinear` gradient's Y-direction, defaults to [0.5, 0.0] -> [0.5, 1.0]. 124 | /// Ignored for other gradient types. 125 | open var startPoint2: CGPoint = CGPoint(x: 0, y: 0) { 126 | didSet { 127 | gradientLayer.startPoint2 = startPoint2 128 | } 129 | } 130 | 131 | /// The end point of the `.bilinear` gradient's Y-direction, defaults to [0.5, 0.0] -> [0.5, 1.0]. 132 | /// Ignored for other gradient types. 133 | open var endPoint2: CGPoint = CGPoint(x: 1, y: 1) { 134 | didSet { 135 | gradientLayer.endPoint2 = endPoint2 136 | } 137 | } 138 | 139 | /// Type of the gradient to be drawn. 140 | open var type: GradientType = .linear { 141 | didSet { 142 | gradientLayer.type = type 143 | } 144 | } 145 | 146 | private var gradientLayer: GradientLayer { 147 | return layer as! GradientLayer 148 | } 149 | 150 | open override class var layerClass: AnyClass { 151 | return GradientLayer.self 152 | } 153 | 154 | public override init(frame: CGRect) { 155 | super.init(frame: frame) 156 | commonInit() 157 | } 158 | 159 | public required init?(coder aDecoder: NSCoder) { 160 | super.init(coder: aDecoder) 161 | commonInit() 162 | } 163 | 164 | private func commonInit() { 165 | // layer.contentsScale = UIScreen.mainScreen().scale 166 | layer.needsDisplayOnBoundsChange = true 167 | layer.setNeedsDisplay() 168 | } 169 | 170 | open override func prepareForInterfaceBuilder() { 171 | // To improve IB performance, reduce generated image size 172 | layer.contentsScale = 0.25 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.0 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "MKGradientView", 7 | platforms: [ 8 | .iOS(.v8), 9 | ], 10 | products: [ 11 | .library( 12 | name: "MKGradientView", 13 | targets: ["MKGradientView"] 14 | ), 15 | ], 16 | targets: [ 17 | .target( 18 | name: "MKGradientView", 19 | path: "MKGradientView" 20 | ), 21 | ] 22 | ) 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MKGradientView 2 | 3 | [![Language](http://img.shields.io/badge/language-swift-brightgreen.svg?style=flat)](https://developer.apple.com/swift) 4 | [![Platform](https://img.shields.io/cocoapods/p/MKGradientView.svg?style=flat)](http://cocoapods.org/pods/MKGradientView) 5 | [![License](https://img.shields.io/cocoapods/l/MKGradientView.svg?style=flat)](http://cocoapods.org/pods/MKGradientView) 6 | [![Version](https://img.shields.io/cocoapods/v/MKGradientView.svg?style=flat)](http://cocoapods.org/pods/MKGradientView) 7 | [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) 8 | 9 | Highly customizable Core Graphics based gradient view 10 | 11 | MKGradientView 12 | 13 | 14 | ## Features 15 | #### Available gradient types: 16 | - Linear (Axial) 17 | - Radial (Circular) 18 | - Conical (Angular) 19 | - Bilinear (Four Point) 20 | 21 | ## Installation 22 | ### CocoaPods 23 | 24 | To install `MKGradientView` via [CocoaPods](http://cocoapods.org), add the following line to your Podfile: 25 | 26 | ``` 27 | pod 'MKGradientView' 28 | ``` 29 | 30 | ### Carthage 31 | 32 | To install `MKGradientView` via [Carthage](https://github.com/Carthage/Carthage#if-youre-building-for-ios-tvos-or-watchos), add the following line to your Cartfile: 33 | 34 | ``` 35 | github "maxkonovalov/MKGradientView" 36 | ``` 37 | 38 | ## Usage 39 | See example Xcode project 40 | 41 | ### Interface Builder 42 | Simple 2-color gradients can be set up in Interface Builder. Set your view's custom class to `MKGradientView`. 43 | 44 | You can specify the following custom inspectable properties: 45 | - `Gradient Type` 46 | - `Start Color` 47 | - `End Color` 48 | - `Start Point` 49 | - `End Point` 50 | 51 | ### Code 52 | ```swift 53 | let gradientView = GradientView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) 54 | gradientView.type = .linear 55 | gradientView.colors = [.red, .green] 56 | gradientView.locations = [0, 1] 57 | gradientView.startPoint = CGPoint(x: 0, y: 0) 58 | gradientView.endPoint = CGPoint(x: 1, y: 1) 59 | view.addSubview(gradientView) 60 | ``` 61 | To set up a `Bilinear` gradient, you need to specify `colors2` array: 62 | 63 | ```swift 64 | let gradientView = GradientView(frame: CGRect(x: 100, y: 0, width: 100, height: 100)) 65 | gradientView.type = .bilinear 66 | gradientView.colors = [.red, .yellow] 67 | gradientView.colors2 = [.blue, .cyan] 68 | view.addSubview(gradientView) 69 | ``` 70 | 71 | ## Performance 72 | `GradientLayer` is image-backed, and the algorithm performance behind it highly depends on the size of the generated image. To speed it up, minimum possible layer size should be used. It can be adjusted with `GradientLayer`'s `contentsScale` property, `1.0` being an optimal value for performance/quality balance. Use lower values like `0.5` to speed up rendering time or set it equal to `UIScreen.main.scale` to get precise result. 73 | 74 | 75 | ## Requirements 76 | - iOS 8.0 77 | - Xcode 10, Swift 4+ 78 | 79 | ## License 80 | `MKGradientView` is available under the MIT license. See the LICENSE file for more info. 81 | --------------------------------------------------------------------------------