Created
April 14, 2017 00:11
-
-
Save egcode/1cee3fca4b3463c206a88bcb5978fe1a 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 InspectableUIView: UIView { | |
@IBInspectable var cornerRadius: CGFloat = 0 { | |
didSet { | |
// layer.cornerRadius = CGRectGetHeight(self.bounds) / 2.0//Circle | |
layer.cornerRadius = cornerRadius | |
// layer.masksToBounds = cornerRadius > 0 | |
} | |
} | |
@IBInspectable var borderColor: UIColor = UIColor.clear { | |
didSet { | |
layer.borderColor = borderColor.cgColor | |
} | |
} | |
@IBInspectable var borderWidth: CGFloat = 0 { | |
didSet { | |
layer.borderWidth = borderWidth | |
} | |
} | |
//Gradient | |
let layerGradient = CAGradientLayer() | |
@IBInspectable var gradientStartColor: UIColor = UIColor.black { | |
didSet { | |
configure() | |
} | |
} | |
@IBInspectable var gradientEndColor: UIColor = UIColor.white { | |
didSet { | |
configure() | |
} | |
} | |
//Shadow | |
@IBInspectable var shadowColor: UIColor = UIColor.clear { | |
didSet { | |
layer.shadowColor = shadowColor.cgColor | |
} | |
} | |
@IBInspectable var shadowRadius: CGFloat = 0 { | |
didSet { | |
layer.shadowRadius = shadowRadius | |
} | |
} | |
@IBInspectable var shadowOpacity: Float = 0 { | |
didSet { | |
layer.shadowOpacity = shadowOpacity | |
} | |
} | |
@IBInspectable var shadowOffset: CGSize = CGSize.zero { | |
didSet { | |
layer.shadowOffset = shadowOffset | |
} | |
} | |
//MARK: - init | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
setup() | |
configure() | |
} | |
override func prepareForInterfaceBuilder() { | |
super.prepareForInterfaceBuilder() | |
setup() | |
configure() | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
//Gradient layer frame | |
layerGradient.frame = CGRect( | |
x: 0, | |
y: 0, | |
width: self.bounds.width, | |
height: self.bounds.midY + self.bounds.midY/2.0) | |
} | |
//MARK: - Setup Configure | |
func setup() { | |
//Setup Gradient | |
layer.addSublayer(layerGradient) | |
layerGradient.masksToBounds = true | |
layerGradient.cornerRadius = self.layer.cornerRadius | |
} | |
func configure() { | |
// Configure Gradient | |
layerGradient.colors = [ | |
gradientStartColor.cgColor, | |
gradientEndColor.cgColor | |
] | |
layerGradient.startPoint = CGPoint(x: 0.5, y: 0.0) | |
layerGradient.endPoint = CGPoint(x: 0.5, y: 1.0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment