Created
September 11, 2018 04:25
-
-
Save pddkhanh/c9e9a22e2131248f050ef27b901329d2 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
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