Last active
January 16, 2017 14:34
-
-
Save ysoftware/ab532d3578f82370a3875ded7c9b80f6 to your computer and use it in GitHub Desktop.
Views with simple effects
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
@IBDesignable class GradientView: RoundedView { | |
@IBInspectable var topColor:UIColor = .clear { didSet { setNeedsLayout() } } | |
@IBInspectable var bottomColor:UIColor = .black { didSet { setNeedsLayout() } } | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
let gradient: CAGradientLayer = CAGradientLayer() | |
gradient.frame = bounds | |
gradient.colors = [topColor.cgColor, bottomColor.cgColor] | |
layer.insertSublayer(gradient, at: 0) | |
isOpaque = false | |
backgroundColor = .clear | |
} | |
} | |
@IBDesignable class RoundedView: UIView { | |
@IBInspectable var isRelativeRadius:Bool = true { didSet { setNeedsLayout() } } | |
@IBInspectable var cornerRadius:CGFloat = 0.5 { didSet { setNeedsLayout() } } | |
@IBInspectable var borderWidth:CGFloat = 0 { didSet { setNeedsLayout() } } | |
@IBInspectable var borderColor:UIColor = .clear { didSet { setNeedsLayout() } } | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
layer.masksToBounds = true | |
layer.cornerRadius = isRelativeRadius ? frame.height * cornerRadius : cornerRadius | |
layer.borderColor = borderColor.cgColor | |
layer.borderWidth = borderWidth | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment