Skip to content

Instantly share code, notes, and snippets.

@raberm
Last active October 18, 2015 16:36
Show Gist options
  • Save raberm/4591c73c2d96514f6ab8 to your computer and use it in GitHub Desktop.
Save raberm/4591c73c2d96514f6ab8 to your computer and use it in GitHub Desktop.
@IBDesignable/@IBInspectable GradientView (linear gradient, 2 stops)
// ©2015 Massimiliano Raber, MIT license
// @IBDesignable/@IBInspectable GradientView (linear gradient, 2 stops)
import UIKit
@IBDesignable
class GradientView: UIView {
@IBInspectable var startColor: UIColor = UIColor.whiteColor() {
didSet {
gradientLayer.colors = [ startColor.CGColor, endColor.CGColor ]
}
}
@IBInspectable var endColor: UIColor = UIColor.blackColor() {
didSet {
gradientLayer.colors = [ startColor.CGColor, endColor.CGColor ]
}
}
@IBInspectable var startPoint: CGPoint = CGPoint(x:0.5, y:0) {
didSet {
gradientLayer.startPoint = startPoint
}
}
@IBInspectable var endPoint: CGPoint = CGPoint(x:0.5, y:1) {
didSet {
gradientLayer.endPoint = endPoint
}
}
private var gradientLayer: CAGradientLayer = CAGradientLayer()
override class func layerClass() -> AnyClass {
return CAGradientLayer.self
}
private func setupLayer() {
self.layer.insertSublayer(gradientLayer, atIndex: 0)
}
override init(frame: CGRect) {
super.init(frame: frame)
setupLayer()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupLayer()
}
override func layoutSubviews() {
gradientLayer.frame = self.bounds
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment