├── .gitignore ├── CHANGELOG.md ├── Example └── GradientKit │ ├── GradientKit.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── GradientKit │ ├── AppDelegate.swift │ ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ ├── PercentageGradientViewController.swift │ └── UniformGradientViewController.swift ├── Images └── gradient_examples.png ├── LICENSE ├── Package.swift ├── README.md ├── Slope.podspec └── Sources └── Slope ├── Gradient.swift ├── GradientView.swift ├── PercentageGradient.swift └── UniformGradient.swift /.gitignore: -------------------------------------------------------------------------------- 1 | ### Objective-C ### 2 | # Xcode 3 | # 4 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 5 | 6 | ## Build generated 7 | build/ 8 | DerivedData/ 9 | 10 | ## Various settings 11 | *.pbxuser 12 | !default.pbxuser 13 | *.mode1v3 14 | !default.mode1v3 15 | *.mode2v3 16 | !default.mode2v3 17 | *.perspectivev3 18 | !default.perspectivev3 19 | xcuserdata/ 20 | 21 | ## Other 22 | *.moved-aside 23 | *.xccheckout 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | *.dSYM.zip 30 | *.dSYM 31 | 32 | # CocoaPods 33 | # 34 | # We recommend against adding the Pods directory to your .gitignore. However 35 | # you should judge for yourself, the pros and cons are mentioned at: 36 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 37 | # 38 | # Pods/ 39 | # 40 | # Add this line if you want to avoid checking in source code from the Xcode workspace 41 | # *.xcworkspace 42 | 43 | # Carthage 44 | # 45 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 46 | # Carthage/Checkouts 47 | 48 | Carthage/Build 49 | 50 | # fastlane 51 | # 52 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 53 | # screenshots whenever they are needed. 54 | # For more information about the recommended setup visit: 55 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 56 | 57 | fastlane/report.xml 58 | fastlane/Preview.html 59 | fastlane/screenshots/**/*.png 60 | fastlane/test_output 61 | 62 | # Code Injection 63 | # 64 | # After new code Injection tools there's a generated folder /iOSInjectionProject 65 | # https://github.com/johnno1962/injectionforxcode 66 | 67 | iOSInjectionProject/ 68 | 69 | ### Objective-C Patch ### 70 | 71 | ### Swift ### 72 | # Xcode 73 | # 74 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 75 | 76 | ## Build generated 77 | 78 | ## Various settings 79 | 80 | ## Other 81 | 82 | ## Obj-C/Swift specific 83 | 84 | ## Playgrounds 85 | timeline.xctimeline 86 | playground.xcworkspace 87 | 88 | # Swift Package Manager 89 | # 90 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 91 | # Packages/ 92 | # Package.pins 93 | # Package.resolved 94 | .build/ 95 | 96 | # CocoaPods 97 | # 98 | # We recommend against adding the Pods directory to your .gitignore. However 99 | # you should judge for yourself, the pros and cons are mentioned at: 100 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 101 | # 102 | # Pods/ 103 | # 104 | # Add this line if you want to avoid checking in source code from the Xcode workspace 105 | # *.xcworkspace 106 | 107 | # Carthage 108 | # 109 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 110 | # Carthage/Checkouts 111 | 112 | 113 | # fastlane 114 | # 115 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 116 | # screenshots whenever they are needed. 117 | # For more information about the recommended setup visit: 118 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 119 | 120 | 121 | ### Xcode ### 122 | # Xcode 123 | # 124 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 125 | 126 | ## User settings 127 | 128 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 129 | 130 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 131 | 132 | ### Xcode Patch ### 133 | *.xcodeproj/* 134 | !*.xcodeproj/project.pbxproj 135 | !*.xcodeproj/xcshareddata/ 136 | !*.xcworkspace/contents.xcworkspacedata 137 | /*.gcno 138 | 139 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 1.3.0 (2020-01-19) 2 | 3 | - Adding Swift 5.1 support. 4 | - Adding SPM support. 5 | 6 | # 1.2 (2018-12-26) 7 | 8 | - Updated to Swift 4.2. 9 | 10 | # 1.1 (2018-08-26) 11 | 12 | #### Thanks to [@adamstener](https://github.com/adamstener) for the big help with this release! 13 | --- 14 | 15 | - `GradientAngle` has been added. You can choose from four predefined angles (`.vertical`, `.horizontal`, `.bottomLeftToTopRight`, `topLeftToBottomRight`) or initialize your own start and end points. 16 | 17 | ### Breaking changes 18 | 19 | - `GradientDirection` has been removed. If you need to use `GradientDirection.darkToLight`, instead use the `.reversed` property of `GradientAngle`. 20 | -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B701BA6820ED0D4500BC07A3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B701BA6720ED0D4500BC07A3 /* AppDelegate.swift */; }; 11 | B701BA6A20ED0D4500BC07A3 /* UniformGradientViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B701BA6920ED0D4500BC07A3 /* UniformGradientViewController.swift */; }; 12 | B701BA6D20ED0D4500BC07A3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B701BA6B20ED0D4500BC07A3 /* Main.storyboard */; }; 13 | B701BA6F20ED0D4800BC07A3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B701BA6E20ED0D4800BC07A3 /* Assets.xcassets */; }; 14 | B701BA7220ED0D4800BC07A3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B701BA7020ED0D4800BC07A3 /* LaunchScreen.storyboard */; }; 15 | B7953EE020ED14D6004AB3DD /* PercentageGradientViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7953EDF20ED14D6004AB3DD /* PercentageGradientViewController.swift */; }; 16 | B7BC3CE923D50899008BFF18 /* PercentageGradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7BC3CE523D50898008BFF18 /* PercentageGradient.swift */; }; 17 | B7BC3CEA23D50899008BFF18 /* Gradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7BC3CE623D50898008BFF18 /* Gradient.swift */; }; 18 | B7BC3CEB23D50899008BFF18 /* UniformGradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7BC3CE723D50898008BFF18 /* UniformGradient.swift */; }; 19 | B7BC3CEC23D50899008BFF18 /* GradientView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7BC3CE823D50898008BFF18 /* GradientView.swift */; }; 20 | /* End PBXBuildFile section */ 21 | 22 | /* Begin PBXFileReference section */ 23 | B701BA6420ED0D4500BC07A3 /* GradientKit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GradientKit.app; sourceTree = BUILT_PRODUCTS_DIR; }; 24 | B701BA6720ED0D4500BC07A3 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 25 | B701BA6920ED0D4500BC07A3 /* UniformGradientViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniformGradientViewController.swift; sourceTree = ""; }; 26 | B701BA6C20ED0D4500BC07A3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 27 | B701BA6E20ED0D4800BC07A3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | B701BA7120ED0D4800BC07A3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | B701BA7320ED0D4800BC07A3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | B7953EDF20ED14D6004AB3DD /* PercentageGradientViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PercentageGradientViewController.swift; sourceTree = ""; }; 31 | B7BC3CE523D50898008BFF18 /* PercentageGradient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PercentageGradient.swift; sourceTree = ""; }; 32 | B7BC3CE623D50898008BFF18 /* Gradient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Gradient.swift; sourceTree = ""; }; 33 | B7BC3CE723D50898008BFF18 /* UniformGradient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UniformGradient.swift; sourceTree = ""; }; 34 | B7BC3CE823D50898008BFF18 /* GradientView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GradientView.swift; sourceTree = ""; }; 35 | /* End PBXFileReference section */ 36 | 37 | /* Begin PBXFrameworksBuildPhase section */ 38 | B701BA6120ED0D4500BC07A3 /* Frameworks */ = { 39 | isa = PBXFrameworksBuildPhase; 40 | buildActionMask = 2147483647; 41 | files = ( 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | B701BA5B20ED0D4500BC07A3 = { 49 | isa = PBXGroup; 50 | children = ( 51 | B701BA6620ED0D4500BC07A3 /* GradientKit */, 52 | B701BA6520ED0D4500BC07A3 /* Products */, 53 | ); 54 | sourceTree = ""; 55 | }; 56 | B701BA6520ED0D4500BC07A3 /* Products */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | B701BA6420ED0D4500BC07A3 /* GradientKit.app */, 60 | ); 61 | name = Products; 62 | sourceTree = ""; 63 | }; 64 | B701BA6620ED0D4500BC07A3 /* GradientKit */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | B7BC3CE323D50898008BFF18 /* Sources */, 68 | B701BA6720ED0D4500BC07A3 /* AppDelegate.swift */, 69 | B701BA6920ED0D4500BC07A3 /* UniformGradientViewController.swift */, 70 | B7953EDF20ED14D6004AB3DD /* PercentageGradientViewController.swift */, 71 | B701BA6B20ED0D4500BC07A3 /* Main.storyboard */, 72 | B701BA6E20ED0D4800BC07A3 /* Assets.xcassets */, 73 | B701BA7020ED0D4800BC07A3 /* LaunchScreen.storyboard */, 74 | B701BA7320ED0D4800BC07A3 /* Info.plist */, 75 | ); 76 | path = GradientKit; 77 | sourceTree = ""; 78 | }; 79 | B7BC3CE323D50898008BFF18 /* Sources */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | B7BC3CE423D50898008BFF18 /* Slope */, 83 | ); 84 | name = Sources; 85 | path = ../../../Sources; 86 | sourceTree = ""; 87 | }; 88 | B7BC3CE423D50898008BFF18 /* Slope */ = { 89 | isa = PBXGroup; 90 | children = ( 91 | B7BC3CE523D50898008BFF18 /* PercentageGradient.swift */, 92 | B7BC3CE623D50898008BFF18 /* Gradient.swift */, 93 | B7BC3CE723D50898008BFF18 /* UniformGradient.swift */, 94 | B7BC3CE823D50898008BFF18 /* GradientView.swift */, 95 | ); 96 | path = Slope; 97 | sourceTree = ""; 98 | }; 99 | /* End PBXGroup section */ 100 | 101 | /* Begin PBXNativeTarget section */ 102 | B701BA6320ED0D4500BC07A3 /* GradientKit */ = { 103 | isa = PBXNativeTarget; 104 | buildConfigurationList = B701BA7620ED0D4800BC07A3 /* Build configuration list for PBXNativeTarget "GradientKit" */; 105 | buildPhases = ( 106 | B701BA6020ED0D4500BC07A3 /* Sources */, 107 | B701BA6120ED0D4500BC07A3 /* Frameworks */, 108 | B701BA6220ED0D4500BC07A3 /* Resources */, 109 | ); 110 | buildRules = ( 111 | ); 112 | dependencies = ( 113 | ); 114 | name = GradientKit; 115 | productName = GradientKit; 116 | productReference = B701BA6420ED0D4500BC07A3 /* GradientKit.app */; 117 | productType = "com.apple.product-type.application"; 118 | }; 119 | /* End PBXNativeTarget section */ 120 | 121 | /* Begin PBXProject section */ 122 | B701BA5C20ED0D4500BC07A3 /* Project object */ = { 123 | isa = PBXProject; 124 | attributes = { 125 | LastSwiftUpdateCheck = 0940; 126 | LastUpgradeCheck = 0940; 127 | ORGANIZATIONNAME = Mergesort; 128 | TargetAttributes = { 129 | B701BA6320ED0D4500BC07A3 = { 130 | CreatedOnToolsVersion = 9.4.1; 131 | LastSwiftMigration = 1130; 132 | }; 133 | }; 134 | }; 135 | buildConfigurationList = B701BA5F20ED0D4500BC07A3 /* Build configuration list for PBXProject "GradientKit" */; 136 | compatibilityVersion = "Xcode 9.3"; 137 | developmentRegion = en; 138 | hasScannedForEncodings = 0; 139 | knownRegions = ( 140 | en, 141 | Base, 142 | ); 143 | mainGroup = B701BA5B20ED0D4500BC07A3; 144 | productRefGroup = B701BA6520ED0D4500BC07A3 /* Products */; 145 | projectDirPath = ""; 146 | projectRoot = ""; 147 | targets = ( 148 | B701BA6320ED0D4500BC07A3 /* GradientKit */, 149 | ); 150 | }; 151 | /* End PBXProject section */ 152 | 153 | /* Begin PBXResourcesBuildPhase section */ 154 | B701BA6220ED0D4500BC07A3 /* Resources */ = { 155 | isa = PBXResourcesBuildPhase; 156 | buildActionMask = 2147483647; 157 | files = ( 158 | B701BA7220ED0D4800BC07A3 /* LaunchScreen.storyboard in Resources */, 159 | B701BA6F20ED0D4800BC07A3 /* Assets.xcassets in Resources */, 160 | B701BA6D20ED0D4500BC07A3 /* Main.storyboard in Resources */, 161 | ); 162 | runOnlyForDeploymentPostprocessing = 0; 163 | }; 164 | /* End PBXResourcesBuildPhase section */ 165 | 166 | /* Begin PBXSourcesBuildPhase section */ 167 | B701BA6020ED0D4500BC07A3 /* Sources */ = { 168 | isa = PBXSourcesBuildPhase; 169 | buildActionMask = 2147483647; 170 | files = ( 171 | B7BC3CEA23D50899008BFF18 /* Gradient.swift in Sources */, 172 | B701BA6A20ED0D4500BC07A3 /* UniformGradientViewController.swift in Sources */, 173 | B7BC3CEB23D50899008BFF18 /* UniformGradient.swift in Sources */, 174 | B7953EE020ED14D6004AB3DD /* PercentageGradientViewController.swift in Sources */, 175 | B7BC3CEC23D50899008BFF18 /* GradientView.swift in Sources */, 176 | B701BA6820ED0D4500BC07A3 /* AppDelegate.swift in Sources */, 177 | B7BC3CE923D50899008BFF18 /* PercentageGradient.swift in Sources */, 178 | ); 179 | runOnlyForDeploymentPostprocessing = 0; 180 | }; 181 | /* End PBXSourcesBuildPhase section */ 182 | 183 | /* Begin PBXVariantGroup section */ 184 | B701BA6B20ED0D4500BC07A3 /* Main.storyboard */ = { 185 | isa = PBXVariantGroup; 186 | children = ( 187 | B701BA6C20ED0D4500BC07A3 /* Base */, 188 | ); 189 | name = Main.storyboard; 190 | sourceTree = ""; 191 | }; 192 | B701BA7020ED0D4800BC07A3 /* LaunchScreen.storyboard */ = { 193 | isa = PBXVariantGroup; 194 | children = ( 195 | B701BA7120ED0D4800BC07A3 /* Base */, 196 | ); 197 | name = LaunchScreen.storyboard; 198 | sourceTree = ""; 199 | }; 200 | /* End PBXVariantGroup section */ 201 | 202 | /* Begin XCBuildConfiguration section */ 203 | B701BA7420ED0D4800BC07A3 /* Debug */ = { 204 | isa = XCBuildConfiguration; 205 | buildSettings = { 206 | ALWAYS_SEARCH_USER_PATHS = NO; 207 | CLANG_ANALYZER_NONNULL = YES; 208 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 209 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 210 | CLANG_CXX_LIBRARY = "libc++"; 211 | CLANG_ENABLE_MODULES = YES; 212 | CLANG_ENABLE_OBJC_ARC = YES; 213 | CLANG_ENABLE_OBJC_WEAK = YES; 214 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 215 | CLANG_WARN_BOOL_CONVERSION = YES; 216 | CLANG_WARN_COMMA = YES; 217 | CLANG_WARN_CONSTANT_CONVERSION = YES; 218 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 219 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 220 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 221 | CLANG_WARN_EMPTY_BODY = YES; 222 | CLANG_WARN_ENUM_CONVERSION = YES; 223 | CLANG_WARN_INFINITE_RECURSION = YES; 224 | CLANG_WARN_INT_CONVERSION = YES; 225 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 226 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 227 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 228 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 229 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 230 | CLANG_WARN_STRICT_PROTOTYPES = YES; 231 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 232 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 233 | CLANG_WARN_UNREACHABLE_CODE = YES; 234 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 235 | CODE_SIGN_IDENTITY = "iPhone Developer"; 236 | COPY_PHASE_STRIP = NO; 237 | DEBUG_INFORMATION_FORMAT = dwarf; 238 | ENABLE_STRICT_OBJC_MSGSEND = YES; 239 | ENABLE_TESTABILITY = YES; 240 | GCC_C_LANGUAGE_STANDARD = gnu11; 241 | GCC_DYNAMIC_NO_PIC = NO; 242 | GCC_NO_COMMON_BLOCKS = YES; 243 | GCC_OPTIMIZATION_LEVEL = 0; 244 | GCC_PREPROCESSOR_DEFINITIONS = ( 245 | "DEBUG=1", 246 | "$(inherited)", 247 | ); 248 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 249 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 250 | GCC_WARN_UNDECLARED_SELECTOR = YES; 251 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 252 | GCC_WARN_UNUSED_FUNCTION = YES; 253 | GCC_WARN_UNUSED_VARIABLE = YES; 254 | IPHONEOS_DEPLOYMENT_TARGET = 11.4; 255 | MTL_ENABLE_DEBUG_INFO = YES; 256 | ONLY_ACTIVE_ARCH = YES; 257 | SDKROOT = iphoneos; 258 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 259 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 260 | }; 261 | name = Debug; 262 | }; 263 | B701BA7520ED0D4800BC07A3 /* Release */ = { 264 | isa = XCBuildConfiguration; 265 | buildSettings = { 266 | ALWAYS_SEARCH_USER_PATHS = NO; 267 | CLANG_ANALYZER_NONNULL = YES; 268 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 269 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 270 | CLANG_CXX_LIBRARY = "libc++"; 271 | CLANG_ENABLE_MODULES = YES; 272 | CLANG_ENABLE_OBJC_ARC = YES; 273 | CLANG_ENABLE_OBJC_WEAK = YES; 274 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 275 | CLANG_WARN_BOOL_CONVERSION = YES; 276 | CLANG_WARN_COMMA = YES; 277 | CLANG_WARN_CONSTANT_CONVERSION = YES; 278 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 279 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 280 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 281 | CLANG_WARN_EMPTY_BODY = YES; 282 | CLANG_WARN_ENUM_CONVERSION = YES; 283 | CLANG_WARN_INFINITE_RECURSION = YES; 284 | CLANG_WARN_INT_CONVERSION = YES; 285 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 286 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 287 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 288 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 289 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 290 | CLANG_WARN_STRICT_PROTOTYPES = YES; 291 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 292 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 293 | CLANG_WARN_UNREACHABLE_CODE = YES; 294 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 295 | CODE_SIGN_IDENTITY = "iPhone Developer"; 296 | COPY_PHASE_STRIP = NO; 297 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 298 | ENABLE_NS_ASSERTIONS = NO; 299 | ENABLE_STRICT_OBJC_MSGSEND = YES; 300 | GCC_C_LANGUAGE_STANDARD = gnu11; 301 | GCC_NO_COMMON_BLOCKS = YES; 302 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 303 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 304 | GCC_WARN_UNDECLARED_SELECTOR = YES; 305 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 306 | GCC_WARN_UNUSED_FUNCTION = YES; 307 | GCC_WARN_UNUSED_VARIABLE = YES; 308 | IPHONEOS_DEPLOYMENT_TARGET = 11.4; 309 | MTL_ENABLE_DEBUG_INFO = NO; 310 | SDKROOT = iphoneos; 311 | SWIFT_COMPILATION_MODE = wholemodule; 312 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 313 | VALIDATE_PRODUCT = YES; 314 | }; 315 | name = Release; 316 | }; 317 | B701BA7720ED0D4800BC07A3 /* Debug */ = { 318 | isa = XCBuildConfiguration; 319 | buildSettings = { 320 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 321 | CODE_SIGN_STYLE = Automatic; 322 | DEVELOPMENT_TEAM = 78QN6DA972; 323 | INFOPLIST_FILE = GradientKit/Info.plist; 324 | LD_RUNPATH_SEARCH_PATHS = ( 325 | "$(inherited)", 326 | "@executable_path/Frameworks", 327 | ); 328 | PRODUCT_BUNDLE_IDENTIFIER = com.mergesort.GradientKit; 329 | PRODUCT_NAME = "$(TARGET_NAME)"; 330 | SWIFT_VERSION = 5.0; 331 | TARGETED_DEVICE_FAMILY = "1,2"; 332 | }; 333 | name = Debug; 334 | }; 335 | B701BA7820ED0D4800BC07A3 /* Release */ = { 336 | isa = XCBuildConfiguration; 337 | buildSettings = { 338 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 339 | CODE_SIGN_STYLE = Automatic; 340 | DEVELOPMENT_TEAM = 78QN6DA972; 341 | INFOPLIST_FILE = GradientKit/Info.plist; 342 | LD_RUNPATH_SEARCH_PATHS = ( 343 | "$(inherited)", 344 | "@executable_path/Frameworks", 345 | ); 346 | PRODUCT_BUNDLE_IDENTIFIER = com.mergesort.GradientKit; 347 | PRODUCT_NAME = "$(TARGET_NAME)"; 348 | SWIFT_VERSION = 5.0; 349 | TARGETED_DEVICE_FAMILY = "1,2"; 350 | }; 351 | name = Release; 352 | }; 353 | /* End XCBuildConfiguration section */ 354 | 355 | /* Begin XCConfigurationList section */ 356 | B701BA5F20ED0D4500BC07A3 /* Build configuration list for PBXProject "GradientKit" */ = { 357 | isa = XCConfigurationList; 358 | buildConfigurations = ( 359 | B701BA7420ED0D4800BC07A3 /* Debug */, 360 | B701BA7520ED0D4800BC07A3 /* Release */, 361 | ); 362 | defaultConfigurationIsVisible = 0; 363 | defaultConfigurationName = Release; 364 | }; 365 | B701BA7620ED0D4800BC07A3 /* Build configuration list for PBXNativeTarget "GradientKit" */ = { 366 | isa = XCConfigurationList; 367 | buildConfigurations = ( 368 | B701BA7720ED0D4800BC07A3 /* Debug */, 369 | B701BA7820ED0D4800BC07A3 /* Release */, 370 | ); 371 | defaultConfigurationIsVisible = 0; 372 | defaultConfigurationName = Release; 373 | }; 374 | /* End XCConfigurationList section */ 375 | }; 376 | rootObject = B701BA5C20ED0D4500BC07A3 /* Project object */; 377 | } 378 | -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // GradientKit 4 | // 5 | // Created by Joe Fabisevich on 7/4/18. 6 | // Copyright © 2018 Mergesort. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 18 | // 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 | -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit/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 | -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit/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 | -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit/PercentageGradientViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PercentageGradientViewController.swift 3 | // GradientKit 4 | // 5 | // Created by Joe Fabisevich on 7/4/18. 6 | // Copyright © 2018 Mergesort. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class PercentageGradientViewController: UIViewController { 12 | 13 | private let backgroundGradientView: GradientView = { 14 | let gradientView = GradientView() 15 | gradientView.gradient = PercentageGradient(baseColor: #colorLiteral(red: 0.05882352941, green: 0.4509803922, blue: 0.6980392157, alpha: 1), angle: .horizontal, percentage: 0.2) 16 | 17 | return gradientView 18 | }() 19 | 20 | override func viewDidLoad() { 21 | super.viewDidLoad() 22 | 23 | self.backgroundGradientView.translatesAutoresizingMaskIntoConstraints = false 24 | self.view.addSubview(self.backgroundGradientView) 25 | 26 | let backgroundConstraints = [ 27 | self.backgroundGradientView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor), 28 | self.backgroundGradientView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor), 29 | self.backgroundGradientView.topAnchor.constraint(equalTo: self.view.topAnchor), 30 | self.backgroundGradientView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor), 31 | ] 32 | 33 | NSLayoutConstraint.activate(backgroundConstraints) 34 | } 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Example/GradientKit/GradientKit/UniformGradientViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UniformGradientViewController.swift 3 | // GradientKit 4 | // 5 | // Created by Joe Fabisevich on 7/4/18. 6 | // Copyright © 2018 Mergesort. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class UniformGradientViewController: UIViewController { 12 | 13 | private let backgroundGradientView: GradientView = { 14 | let gradientView = GradientView() 15 | gradientView.gradient = UniformGradient(colors: [#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1), #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)], angle: GradientAngle.vertical.reversed) 16 | 17 | return gradientView 18 | }() 19 | 20 | override func viewDidLoad() { 21 | super.viewDidLoad() 22 | 23 | self.backgroundGradientView.translatesAutoresizingMaskIntoConstraints = false 24 | self.view.addSubview(self.backgroundGradientView) 25 | 26 | let backgroundConstraints = [ 27 | self.backgroundGradientView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor), 28 | self.backgroundGradientView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor), 29 | self.backgroundGradientView.topAnchor.constraint(equalTo: self.view.topAnchor), 30 | self.backgroundGradientView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor), 31 | ] 32 | 33 | NSLayoutConstraint.activate(backgroundConstraints) 34 | } 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Images/gradient_examples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mergesort/Slope/d749b9a6c99348a83b0e652932163a72f7020c94/Images/gradient_examples.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Joe Fabisevich 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.0 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "Slope", 7 | platforms: [ 8 | .iOS(.v9) 9 | ], 10 | products: [ 11 | .library( 12 | name: "Slope", 13 | targets: ["Slope"]), 14 | ], 15 | dependencies: [], 16 | targets: [ 17 | .target( 18 | name: "Slope", 19 | dependencies: []) 20 | ] 21 | ) 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Slope 2 | 3 | #### Flat is out, so let's make depth easy. 4 | 5 | --- 6 | 7 | [![Pod Version](https://img.shields.io/badge/Pod-1.3.0-6193DF.svg)](https://cocoapods.org/) 8 | ![Swift Version](https://img.shields.io/badge/Swift-5.1-brightgreen.svg) 9 | ![License MIT](https://img.shields.io/badge/License-MIT-lightgrey.svg) 10 | ![Plaform](https://img.shields.io/badge/Platform-iOS-lightgrey.svg) 11 | 12 | ### Gradients are coming back in style, so let's party like it's 1989 again. 13 | 14 | Use them for backgrounds, use them for UI elements, use them to make yourself happy. 15 | 16 | ![](Images/gradient_examples.png) 17 | 18 | --- 19 | 20 | The built in `CAGradientLayer` API is overly complex, doesn't work with auto layout, and is very fiddly. 21 | 22 | Using Slope is simple. 23 | 24 | Simple is better than complex! 25 | 26 | #### Let's build a gradient view like above: 27 | 28 | ```swift 29 | import Slope 30 | 31 | let gradientView = GradientView() 32 | gradientView.gradient = UniformGradient(colors: [.darkGray, .lightGray]) 33 | ``` 34 | 35 | There is no step 2. You now have a `UIView` subclass that you can add to your screen. 36 | 37 | #### Let's build the save button above: 38 | 39 | We're going to make it a little nicer by adding a highlighting effect when you touch down. 40 | 41 | ```swift 42 | final class GradientButton: UIControl { 43 | 44 | let titleLabel: UILabel = { 45 | let label = UILabel() 46 | label.textColor = UIColor.white 47 | label.font = UIFont.systemFont(ofSize: 24.0) 48 | label.textAlignment = .center 49 | 50 | return label 51 | }() 52 | 53 | override var tintColor: UIColor! { 54 | didSet { 55 | self.backgroundColor = self.tintColor 56 | self.highlightedColor = self.tintColor.darkened(byPercentage: 0.1) 57 | 58 | let lightGreen = #colorLiteral(red: 0, green: 0.8235294118, blue: 0.5764705882, alpha: 1) 59 | self.backgroundGradientView.gradient = PercentageGradient(baseColor: lightGreen, percentage: 0.06) 60 | } 61 | } 62 | 63 | private let backgroundGradientView = GradientView() 64 | 65 | private var highlightedColor: UIColor? 66 | 67 | // MARK: Initializers 68 | 69 | override init(frame: CGRect) { 70 | super.init(frame: frame) 71 | 72 | self.setup() 73 | } 74 | 75 | @available(*, unavailable) 76 | required init?(coder aDecoder: NSCoder) { 77 | fatalError("init(coder:) has not been implemented") 78 | } 79 | 80 | // MARK: Touch down effects 81 | 82 | override func touchesBegan(_ touches: Set, with event: UIEvent?) { 83 | super.touchesBegan(touches, with: event) 84 | 85 | self.backgroundColor = self.highlightedColor 86 | self.backgroundGradientView.tintColor = self.highlightedColor ?? self.tintColor 87 | } 88 | 89 | override func touchesEnded(_ touches: Set, with event: UIEvent?) { 90 | super.touchesEnded(touches, with: event) 91 | 92 | self.backgroundColor = self.tintColor 93 | self.backgroundGradientView.tintColor = self.tintColor 94 | } 95 | 96 | override func touchesCancelled(_ touches: Set, with event: UIEvent?) { 97 | super.touchesCancelled(touches, with: event) 98 | 99 | self.backgroundColor = self.tintColor 100 | self.backgroundGradientView.tintColor = self.tintColor 101 | } 102 | } 103 | 104 | private extension GradientButton { 105 | 106 | func setup() { 107 | self.addSubview(self.backgroundGradientView) 108 | self.backgroundGradientView.pinToSuperview() 109 | 110 | self.addSubview(self.titleLabel) 111 | 112 | self.setupConstraints() 113 | } 114 | 115 | func setupConstraints() { 116 | self.backgroundGradientView.translatesAutoresizingMaskIntoConstraints = false 117 | 118 | let backgroundConstraints = [ 119 | self.backgroundGradientView.leadingAnchor.constraint(equalTo: self.leadingAnchor), 120 | self.backgroundGradientView.trailingAnchor.constraint(equalTo: self.trailingAnchor), 121 | self.backgroundGradientView.topAnchor.constraint(equalTo: self.topAnchor), 122 | self.backgroundGradientView.bottomAnchor.constraint(equalTo: self.bottomAnchor) 123 | ] 124 | 125 | NSLayoutConstraint.activate(backgroundConstraints) 126 | 127 | self.titleLabel.translatesAutoresizingMaskIntoConstraints = false 128 | 129 | let titleLabelConstraints = [ 130 | self.titleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor), 131 | self.titleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor), 132 | self.titleLabel.topAnchor.constraint(equalTo: self.topAnchor), 133 | self.titleLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor) 134 | ] 135 | 136 | NSLayoutConstraint.activate(titleLabelConstraints) 137 | } 138 | 139 | } 140 | ``` 141 | 142 | And voilà, a beautiful gradient button with highlighting that you can reuse across your app. 143 | 144 | --- 145 | 146 | #### Types of Gradients 147 | 148 | There are two current types of gradients, `UniformGradient` and `PercentageGradient`. 149 | 150 | You can create your own as you see fit by conforming to the `Gradient` protocol. 151 | 152 | - If you want to make a radial gradient, go wild. 153 | - If you want to make a diagonal gradient, that works too. 154 | - If you come up with something creative, contribute to the project! 155 | 156 | 157 | ## Installation 158 | 159 | You can use [SPM](https://swift.org/package-manager/) to install `GenericCells`. 160 | 161 | You can also use [CocoaPods](http://cocoapods.org/) to install `Slope` by adding it to your `Podfile`: 162 | 163 | ```swift 164 | platform :ios, '9.0' 165 | use_frameworks! 166 | 167 | pod 'Slope' 168 | ``` 169 | 170 | Or install it manually by downloading `Gradient.swift`, `UniformGradient.swift`, `PercentageGradient.swift`, and `GradientView.swift`, and dropping them in your project. 171 | 172 | ## About me 173 | 174 | Hi, I'm [Joe](http://fabisevi.ch) everywhere on the web, but especially on [Twitter](https://twitter.com/mergesort). 175 | 176 | ## License 177 | 178 | See the [license](LICENSE) for more information about how you can use Slope. 179 | 180 | ## Is that it? 181 | 182 | Yep, that's it. Good night, and have a pleasant tomorrow. 183 | -------------------------------------------------------------------------------- /Slope.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |spec| 2 | spec.name = 'Slope' 3 | spec.summary = 'A simpler way to implement simple gradients on iOS.' 4 | spec.version = '1.3.0' 5 | spec.license = { :type => 'MIT' } 6 | spec.authors = { 'Joe Fabisevich' => 'github@fabisevi.ch' } 7 | spec.source_files = 'Sources/Slope/*.swift' 8 | spec.swift_version = '5.1' 9 | spec.homepage = 'https://github.com/mergesort' 10 | spec.source = { :git => 'https://github.com/mergesort/Slope.git', :tag => "#{spec.version}" } 11 | 12 | spec.ios.deployment_target = '9.0' 13 | spec.requires_arc = true 14 | spec.social_media_url = 'https://twitter.com/mergesort' 15 | spec.framework = 'UIKit' 16 | end 17 | -------------------------------------------------------------------------------- /Sources/Slope/Gradient.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | /// A protocol that serves as the base for constructing concrete `Gradient` types. 4 | public protocol Gradient { 5 | 6 | /// The angle that is needed to construct a `Gradient`. 7 | var angle: GradientAngle { get } 8 | 9 | /// An array of `GradientComponents` that will be used in constructing a full gradient. 10 | /// 11 | /// - Returns: The array of `GradientComponents` which are used to construct a `Gradient`. 12 | func makeGradientComponents() -> [GradientComponents] 13 | 14 | } 15 | 16 | /// The basic component needed to construct a `Gradient`. 17 | /// A `Gradient` is created when you compile an array of `GradientComponents`. 18 | public struct GradientComponents { 19 | public let color: UIColor 20 | public let location: Double 21 | } 22 | 23 | /// A `GradientAngle` defines the shape of a `Gradient`. 24 | public struct GradientAngle { 25 | 26 | /// The start point of the `GradientAngle`. 27 | public let start: CGPoint 28 | 29 | /// The end point of the `GradientAngle`. 30 | public let end: CGPoint 31 | 32 | /// Creates a GradientAngle from a start point and end point. 33 | /// 34 | /// - Parameters: 35 | /// - start: The start point of the `GradientAngle`. 36 | /// - end: The end point of the `GradientAngle`. 37 | init(start: CGPoint, end: CGPoint) { 38 | self.start = start 39 | self.end = end 40 | } 41 | 42 | /// A `GradientAngle` that swaps the end point and start point. 43 | /// This is useful if you want to create a gradient that goes from 44 | /// dark to light rather than light to dark. 45 | public var reversed: GradientAngle { 46 | return GradientAngle(start: self.end, end: self.start) 47 | } 48 | 49 | /// `GradientAngle.vertical` 50 | public static let defaultAngle = GradientAngle.vertical 51 | 52 | /// Used for creating a diagonal gradient that starts in the bottom left corner and goes to the top right corner. 53 | public static let bottomLeftToTopRight: GradientAngle = GradientAngle( 54 | start: CGPoint(x: 0.0, y: 1.0), 55 | end: CGPoint(x: 1.0, y: 0.0) 56 | ) 57 | 58 | /// Used for creating a diagonal gradient that starts in the top left corner and goes to the bottom right corner. 59 | public static let topLeftToBottomRight: GradientAngle = GradientAngle( 60 | start: CGPoint(x: 0.0, y: 0.0), 61 | end: CGPoint(x: 1.0, y: 0.0) 62 | ) 63 | 64 | /// Used for creating a gradient that starts from the left center and ends in the right center. 65 | public static let horizontal: GradientAngle = GradientAngle( 66 | start: CGPoint(x: 0.0, y: 0.5), 67 | end: CGPoint(x: 1.0, y: 0.5) 68 | ) 69 | 70 | /// Used for creating a gradient that starts from the top center and ends in the bottom center. 71 | public static let vertical: GradientAngle = GradientAngle( 72 | start: CGPoint(x: 0.5, y: 0.0), 73 | end: CGPoint(x: 0.5, y: 1.0) 74 | ) 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Sources/Slope/GradientView.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | /// A UIView subclass allowing you to easily create and display a gradient. 4 | public final class GradientView: UIView { 5 | 6 | private let gradientLayer = CAGradientLayer() 7 | 8 | /// The backing `Gradient` which will be displayed in the view. 9 | public var gradient: Gradient = UniformGradient(colors: [.white]) { 10 | didSet { 11 | self.generateGradient() 12 | } 13 | } 14 | 15 | // MARK: Initializers 16 | 17 | public override init(frame: CGRect) { 18 | super.init(frame: frame) 19 | 20 | self.setup() 21 | } 22 | 23 | @available(*, unavailable) 24 | public required init?(coder aDecoder: NSCoder) { 25 | fatalError("init(coder:) has not been implemented") 26 | } 27 | 28 | public override func layoutSubviews() { 29 | super.layoutSubviews() 30 | 31 | self.gradientLayer.frame = self.bounds 32 | } 33 | 34 | } 35 | 36 | private extension GradientView { 37 | 38 | func setup() { 39 | self.isUserInteractionEnabled = false 40 | self.layer.addSublayer(self.gradientLayer) 41 | } 42 | 43 | func generateGradient() { 44 | self.gradientLayer.startPoint = self.gradient.angle.start 45 | self.gradientLayer.endPoint = self.gradient.angle.end 46 | 47 | let components = self.gradient.makeGradientComponents() 48 | let colors = components.map { $0.color.cgColor } 49 | let locations = components.map { NSNumber(value: $0.location) } 50 | 51 | self.gradientLayer.colors = colors 52 | self.gradientLayer.locations = locations 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Sources/Slope/PercentageGradient.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | /// A `Gradient` that uses a base color and a percentage to offset. 4 | public struct PercentageGradient: Gradient { 5 | 6 | /// The color that will serve as the foundation of the gradient. 7 | /// This is the color you will see at the center of the gradient, 8 | /// and be lighter or darker outward from that point. 9 | public let baseColor: UIColor 10 | 11 | public let angle: GradientAngle 12 | 13 | /// The percentage that the `baseColor` will be modified. 14 | public let percentage: CGFloat 15 | 16 | // MARK: Initializers 17 | 18 | public init(baseColor: UIColor, angle: GradientAngle = .defaultAngle, percentage: CGFloat) { 19 | self.angle = angle 20 | self.baseColor = baseColor 21 | self.percentage = percentage 22 | } 23 | 24 | } 25 | 26 | public extension PercentageGradient { 27 | 28 | func makeGradientComponents() -> [GradientComponents] { 29 | return [ 30 | GradientComponents( 31 | color: self.lighterColor(from: baseColor), 32 | location: 0.0 33 | ), 34 | GradientComponents( 35 | color: self.darkerColor(from: baseColor), 36 | location: 1.0 37 | ), 38 | ] 39 | } 40 | 41 | } 42 | 43 | private extension PercentageGradient { 44 | 45 | func lighterColor(from color: UIColor) -> UIColor { 46 | return (color.brightnessOffset(byPercentage: self.percentage) ?? UIColor.white) 47 | } 48 | 49 | func darkerColor(from color: UIColor) -> UIColor { 50 | return (color.brightnessOffset(byPercentage: -self.percentage) ?? UIColor.black) 51 | } 52 | 53 | } 54 | 55 | private extension UIColor { 56 | 57 | func hsba() -> (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)? { 58 | var hue: CGFloat = .nan 59 | var saturation: CGFloat = .nan 60 | var brightness: CGFloat = .nan 61 | var alpha: CGFloat = .nan 62 | 63 | guard self.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) else { return nil } 64 | 65 | return (hue: hue, saturation: saturation, brightness: brightness, alpha: alpha) 66 | } 67 | 68 | func brightnessOffset(byPercentage percent: CGFloat) -> UIColor? { 69 | guard percent != 0 else { return self.copy() as? UIColor } 70 | 71 | guard let hsba = self.hsba() else { return nil } 72 | 73 | let percentage: CGFloat = min(max(percent, -1), 1) 74 | let newBrightness = min(max(hsba.brightness + percentage, -1), 1) 75 | return UIColor(hue: hsba.hue, saturation: hsba.saturation, brightness: newBrightness, alpha: hsba.alpha) 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /Sources/Slope/UniformGradient.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | /// A `Gradient` that evenly distributes it's colors. 4 | public struct UniformGradient: Gradient { 5 | 6 | /// The colors that will be used to construct the gradient with an equal distribution. 7 | public let colors: [UIColor] 8 | 9 | public let angle: GradientAngle 10 | 11 | // MARK: Initializers 12 | 13 | public init(colors: [UIColor], angle: GradientAngle = .defaultAngle) { 14 | self.colors = colors 15 | self.angle = angle 16 | } 17 | 18 | } 19 | 20 | public extension UniformGradient { 21 | 22 | func makeGradientComponents() -> [GradientComponents] { 23 | let min = 0.0 24 | let max = 1.0 25 | let by = (max-min)/Double(self.colors.count - 1) 26 | 27 | let locations = stride(from: min, through: max, by: by) 28 | .map { $0 } 29 | 30 | return zip(colors, locations) 31 | .map { GradientComponents(color: $0, location: $1) } 32 | } 33 | 34 | } 35 | --------------------------------------------------------------------------------