Skip to content

Instantly share code, notes, and snippets.

@ysoftware
Last active January 16, 2017 14:34
Show Gist options
  • Save ysoftware/ab532d3578f82370a3875ded7c9b80f6 to your computer and use it in GitHub Desktop.
Save ysoftware/ab532d3578f82370a3875ded7c9b80f6 to your computer and use it in GitHub Desktop.
Views with simple effects
@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