Reputation: 4462
I have a custom UIButton. I want to add radius to some corners (not to all of them) I'm using UIBezierPath for setting that. But because i'm masking the layer i cant add a shadow.
For all corners it works:
func setupView() {
layer.cornerRadius = 10
addShadow(UIColor.blackColor(), opacity: 0.8, radius: 5, offset: CGSizeMake(1, 1))
}
func addShadow(color: UIColor, opacity: Float, radius: CGFloat, offset: CGSize){
layer.shadowColor = color.CGColor
layer.shadowOpacity = opacity
layer.shadowRadius = radius
layer.shadowOffset = offset
}
For some corners using UIBezierPath i get only the rounded corners but no shadow
func setupView() {
addCornerRadiusToCurners(false, leftTop: true, rightBottom: false, leftBottom: false, radius: 10)
addShadow(UIColor.blackColor(), opacity: 0.8, radius: 5, offset: CGSizeMake(1, 1))
}
func addCornerRadiusToCurners(rightTop: Bool = true, leftTop: Bool = true, rightBottom: Bool = true, leftBottom: Bool = true, radius: CGFloat) {
var corners: UIRectCorner = []
if rightTop { corners.insert(.TopRight) }
if leftTop { corners.insert(.TopLeft) }
if rightBottom { corners.insert(.BottomRight) }
if leftBottom { corners.insert(.BottomLeft) }
let cornerPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(radius, radius))
let cornerMaskLayer = CAShapeLayer()
cornerMaskLayer.path = cornerPath.CGPath
layer.mask = cornerMaskLayer
}
Upvotes: 0
Views: 326
Reputation: 4825
Use this updated CustomButton class:
class CustomButton: UIButton {
var cornerMaskLayer : CAShapeLayer!
var corners: UIRectCorner = []
var cornerRadius : CGFloat = 0
override func awakeFromNib() {
addCornerRadiusToCurners(radius: 10)
addShadow(UIColor.blackColor(), opacity: 0.5, radius: 2.0, offset: CGSizeMake(2, 5))
}
func addShadow(color: UIColor, opacity: Float, radius: CGFloat, offset: CGSize){
layer.shadowColor = color.CGColor
layer.shadowOpacity = opacity
layer.shadowRadius = radius
layer.shadowOffset = offset
}
func setupView() {
addCornerRadiusToCurners(false, leftTop: true, rightBottom: false, leftBottom: false, radius: 10)
addShadow(UIColor.blackColor(), opacity: 0.8, radius: 5, offset: CGSizeMake(1, 1))
}
override func layoutSubviews() {
super.layoutSubviews()
let cornerPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(cornerRadius, cornerRadius))
cornerMaskLayer.path = cornerPath.CGPath
}
func addCornerRadiusToCurners(rightTop: Bool = true, leftTop: Bool = true, rightBottom: Bool = true, leftBottom: Bool = true, radius: CGFloat) {
if rightTop { corners.insert(.TopRight) }
if leftTop { corners.insert(.TopLeft) }
if rightBottom { corners.insert(.BottomRight) }
if leftBottom { corners.insert(.BottomLeft) }
cornerRadius = radius
let cornerPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(radius, radius))
cornerMaskLayer = CAShapeLayer()
cornerMaskLayer.path = cornerPath.CGPath
cornerMaskLayer.fillColor = self.backgroundColor?.CGColor
self.backgroundColor = UIColor.clearColor()
layer.addSublayer(cornerMaskLayer);
}
}
Upvotes: 0