Created
April 14, 2017 18:51
-
-
Save danie7k/450ba64afc5a80da883ee7f030f372d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint) | |
enum GradientOrientation { | |
case horizontal | |
case vertical | |
var startPoint: CGPoint { | |
return points.startPoint | |
} | |
var endPoint: CGPoint { | |
return points.endPoint | |
} | |
var points: GradientPoints { | |
switch self { | |
case .horizontal: | |
return (CGPoint.init(x: 0.0, y: 0.5), CGPoint.init(x: 1.0, y: 0.5)) | |
case .vertical: | |
return (CGPoint.init(x: 0.0, y: 0.0), CGPoint.init(x: 0.0, y: 1.0)) | |
} | |
} | |
} | |
extension UIView { | |
func applyGradient(withColours colours: [UIColor], gradientOrientation orientation: GradientOrientation, locations: [NSNumber]? = nil) { | |
let gradient: CAGradientLayer = CAGradientLayer() | |
gradient.frame = bounds | |
gradient.colors = colours.map { $0.cgColor } | |
gradient.startPoint = orientation.startPoint | |
gradient.endPoint = orientation.endPoint | |
gradient.locations = locations | |
layer.insertSublayer(gradient, at: 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment