Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class GradientButton: UIButton {
- let gradient: CAGradientLayer = CAGradientLayer()
- internal override init(frame: CGRect) {
- super.init(frame: frame)
- }
- internal required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- }
- required init(_ colors: [UIColor], locations: [NSNumber]?) {
- super.init(frame: .zero)
- applyGradient(colors,locations:locations)
- }
- func applyGradient(_ colors: [UIColor], locations: [NSNumber]?) {
- gradient.colors = colors
- gradient.locations = locations
- gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
- gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
- layer.insertSublayer(gradient, at: 0)
- }
- override func layoutSublayers(of layer: CALayer) {
- super.layoutSublayers(of: layer)
- gradient.frame = self.bounds
- }
- }
- let button = GradientButton([.red,.blue], locations: nil)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement