实现方案
需求:视图既需要圆角,又需要阴影
实现重点:
- 不要使用clipToBounds
- 需要绘制一个圆角UIBezierPath。直接使用直接 UIBezierPath 会发现在四个角处的阴影会比较重
- 圆角UIBezierPath 的 cornerRadii 需要与button的圆角大小一样
- (void)makeRoundButtonShadow:(UIButton *)button {
button.layer.cornerRadius = 8;
[button addShadow:UIColor.blackColor shadowOpacity:0.15 shadowRadius:2 shadowOffset:CGSizeZero];
CGRect rect = CGRectMake(0, 0, button.width, button.height);
UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(8, 8)];
button.layer.shadowPath = cornerPath.CGPath;
}
- (void)addShadow:(UIColor *)shadowColor
shadowOpacity:(CGFloat)shadowOpacity
shadowRadius:(CGFloat)shadowRadius
shadowOffset:(CGSize)shadowOffset {
self.layer.shadowColor = shadowColor.CGColor;
self.layer.shadowOffset = shadowOffset;
self.layer.shadowOpacity = shadowOpacity;
self.layer.shadowRadius = shadowRadius;
}