Created
October 3, 2017 08:14
-
-
Save Banck/18a20901bcd6fb872a85b56ed71fcd12 to your computer and use it in GitHub Desktop.
GradientView
This file contains hidden or 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
// | |
// GradientView.swift | |
// Aura | |
// | |
// Created by Egor Sakhabaev on 23.07.17. | |
// Copyright © 2017 Egor Sakhabaev. All rights reserved. | |
// | |
import UIKit | |
@IBDesignable | |
class GradientView: UIView { | |
@IBInspectable var firstColor: UIColor = UIColor.clear { | |
didSet { | |
updateView() | |
} | |
} | |
@IBInspectable var secondColor: UIColor = UIColor.clear { | |
didSet { | |
updateView() | |
} | |
} | |
@IBInspectable var isHorizontal: Bool = true { | |
didSet { | |
updateView() | |
} | |
} | |
override class var layerClass: AnyClass { | |
get { | |
return CAGradientLayer.self | |
} | |
} | |
func updateView() { | |
let layer = self.layer as! CAGradientLayer | |
layer.colors = [firstColor, secondColor].map {$0.cgColor} | |
if (isHorizontal) { | |
layer.startPoint = CGPoint(x: 0, y: 0.5) | |
layer.endPoint = CGPoint (x: 1, y: 0.5) | |
} else { | |
layer.startPoint = CGPoint(x: 0.5, y: 0) | |
layer.endPoint = CGPoint (x: 0.5, y: 1) | |
} | |
} | |
} |
Used this class, but not being able to show color gradient in my view. I do have the option to select first and second color
Used this file in my class project. Beautiful! Thank you so much!
Thanl you man its realy help
Awesome! Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks man 👍