Created
September 11, 2017 10:52
-
-
Save TramPamPam/ba72be6ffc81a1c74e6a415f042412e1 to your computer and use it in GitHub Desktop.
This file contains 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 | |
@IBDesignable | |
class GradientView: RoundedView { | |
@IBInspectable var from: UIColor! = UIColor.init(colorLiteralRed: 255.0/64.0, green: 255.0/164.0, blue: 255.0/196.0, alpha: 1) | |
@IBInspectable var to: UIColor! = UIColor.init(colorLiteralRed: 255.0/59.0, green: 255.0/88.0, blue: 255.0/152.0, alpha: 1) | |
@IBInspectable var vertical: Bool = false | |
@IBInspectable var rounded: Bool = false { | |
didSet { | |
radius = rounded ? 3.0 : 0.0 | |
} | |
} | |
var gradient: CAGradientLayer? { | |
let gradient = CAGradientLayer() | |
gradient.frame = self.bounds | |
gradient.colors = [from!.cgColor, to!.cgColor] | |
if !vertical { | |
gradient.startPoint = CGPoint(x: 0.0, y: 0.5) | |
gradient.endPoint = CGPoint(x: 1.0, y: 0.5) | |
} | |
return gradient | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
guard let gradientLayer = gradient, layer.bounds.size.height < 1000.0 else { | |
return | |
} | |
layer.sublayers = [gradientLayer] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment