Skip to content

Useful extensions for UIViews and CALayer classes to add beautiful color gradients.

License

Notifications You must be signed in to change notification settings

rootstrap/SwiftGradients

Repository files navigation

SwiftGradients

CI Status Version License Platform Carthage SPM Swift Version

What is it?

SwiftGradients gives you useful extensions for UIViews and CALayer classes to add beautiful color gradients.

Installation

1. Cocoapods

pod 'SwiftGradients', '~> 1.0.0'

2. Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. Add the following line to your Cartfile and follow the installation instructions.

github "rootstrap/SwiftGradients" ~> 1.0.0

3. Swift Package Manager

  • In XCode 11, go to File -> Swift Packages -> Add Package Dependency.
  • Enter the repo URL (https://github.com/rootstrap/SwiftGradients) and click Next.
  • Select the version rule desired (you can specify a version number, branch or commit) and click Next.
  • Finally, select the target where you want to use the framework.

That should be it. SwiftGradients should appear in the navigation panel as a dependency and the framework will be linked automatically to your target.

Note: It is always recommended to lock your external libraries to a specific version.

Usage

1. Adding gradients to your subviews

let gradientLayer = view.addGradient(
  colors: [.blue, .green],
  direction: .topToBottom
)

2. Customize options for your gradient

You can set a custom angle(measured in degrees and anti-clockwise), rather that one of the four predefined directions. Also, you can provide the color stop locations for better accuracy.

let gradientLayer = view.addGradient(
  colors: [.blue, .green, .black],
  angle: 45
  locations: [0, 50, 75]
)

Voilà


Example

To run the example project, clone the repo, and run pod install from the Example directory first.

License

SwiftGradients is available under the MIT license. See the LICENSE file for more info.