Skip to content

Instantly share code, notes, and snippets.

@jhays
Last active June 14, 2019 07:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jhays/785983dbea549c0b29e1 to your computer and use it in GitHub Desktop.
Save jhays/785983dbea549c0b29e1 to your computer and use it in GitHub Desktop.
GradientView - Editable in Interface Builder
//
// JHGradientView.swift
// Gradient View editable in Interface Builder
//
// Created by JHays on 2/20/15.
// Copyright (c) 2015 Orbosphere. All rights reserved.
//
import Foundation
import UIKit
@IBDesignable public class JHGradientView: UIView {
@IBInspectable public var topColor: UIColor? {
didSet {
configureGradientView()
}
}
@IBInspectable public var bottomColor: UIColor? {
didSet {
configureGradientView()
}
}
@IBInspectable var startX: CGFloat = 0.0 {
didSet{
configureGradientView()
}
}
@IBInspectable var startY: CGFloat = 1.0 {
didSet{
configureGradientView()
}
}
@IBInspectable var endX: CGFloat = 0.0 {
didSet{
configureGradientView()
}
}
@IBInspectable var endY: CGFloat = 0.0 {
didSet{
configureGradientView()
}
}
override public class func layerClass() -> AnyClass {
return CAGradientLayer.self
}
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configureGradientView()
}
override init(frame: CGRect) {
super.init(frame: frame)
configureGradientView()
}
public override func tintColorDidChange() {
super.tintColorDidChange()
configureGradientView()
}
func configureGradientView() {
let color1 = topColor ?? self.tintColor as UIColor
let color2 = bottomColor ?? UIColor.blackColor() as UIColor
let colors: Array <AnyObject> = [ color1.CGColor, color2.CGColor ]
let layer = self.layer as! CAGradientLayer
layer.colors = colors
layer.startPoint = CGPointMake(startX, startY)
layer.endPoint = CGPointMake(endX, endY)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment