Skip to content

Instantly share code, notes, and snippets.

@pddkhanh
Created September 11, 2018 04:25
Show Gist options
  • Save pddkhanh/c9e9a22e2131248f050ef27b901329d2 to your computer and use it in GitHub Desktop.
Save pddkhanh/c9e9a22e2131248f050ef27b901329d2 to your computer and use it in GitHub Desktop.
public class RoundedShadowView: UIView {
public var cornerRadius: CGFloat = 2.0 {
didSet {
setNeedsLayout()
}
}
public var fillColor: UIColor = .white {
didSet {
shadowLayer.fillColor = fillColor.cgColor
}
}
public var shadowColor: UIColor = .black {
didSet {
shadowLayer.shadowColor = shadowColor.cgColor
}
}
public var shadowOffset: CGSize = CGSize(width: 2, height: 2) {
didSet {
shadowLayer.shadowOffset = shadowOffset
}
}
public var shadowOpacity: Float = 0.2 {
didSet {
shadowLayer.shadowOpacity = shadowOpacity
}
}
public var shadowRadius: CGFloat = 3 {
didSet {
shadowLayer.shadowRadius = shadowRadius
}
}
private var shadowLayer = CAShapeLayer()
public override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
public override func layoutSubviews() {
super.layoutSubviews()
configureShadowLayer()
}
private func setupView() {
layer.insertSublayer(shadowLayer, at: 0)
}
private func configureShadowLayer() {
shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
shadowLayer.fillColor = fillColor.cgColor
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = shadowOffset
shadowLayer.shadowOpacity = shadowOpacity
shadowLayer.shadowRadius = shadowRadius
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment