Skip to content

Instantly share code, notes, and snippets.

@RedGhoul
Last active November 29, 2017 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedGhoul/a0a82a9fb7327ba143742be3a61fbd5e to your computer and use it in GitHub Desktop.
Save RedGhoul/a0a82a9fb7327ba143742be3a61fbd5e to your computer and use it in GitHub Desktop.
import UIKit
//@IBDesignable // allows you to render inside the story board
class GradientView: UIView { // it is inhertating from UIView since that is what we are going to put it in
// these IBInspectablea allow you to assign different colors in the right hand panel
@IBInspectable var topColor: UIColor = UIColor.blue{
didSet{
self.setNeedsLayout()
}
}
@IBInspectable var bottomColor: UIColor = UIColor.green {
didSet{
self.setNeedsLayout()
}
}
override func layoutSubviews() {
let gradientLayer = CAGradientLayer()
// taking the colors from what you set above and giving them to the gradients layer
// you could have also hardcored these values in here as well
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
// specifying where the first and second color is going to be coming from
gradientLayer.startPoint = CGPoint(x:0,y:0)
gradientLayer.endPoint = CGPoint(x:1,y:1)
// giving it the bounds
gradientLayer.frame = self.bounds
// setting the sub layer
self.layer.insertSublayer(gradientLayer, at: 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment