@interface UIView (Add)
- (void)setCornerRadius:(CGFloat)radius withShadow:(BOOL)shadow withOpacity:(CGFloat)opacity;
@end
@implementation UIView (Add)
- (void)setCornerRadius:(CGFloat)radius withShadow:(BOOL)shadow withOpacity:(CGFloat)opacity {
self.layer.cornerRadius = radius;
if (shadow) {
self.layer.shadowColor = [UIColor lightGrayColor].CGColor;
self.layer.shadowOpacity = opacity;
self.layer.shadowOffset = CGSizeMake(-1, 1);
self.layer.shadowRadius = radius;
self.layer.shouldRasterize = NO;
self.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:[self bounds] cornerRadius:radius] CGPath];
}
self.layer.masksToBounds = !shadow;
}
@end
(void)layoutSubviews {
[super layoutSubviews];
[self setCornerRadius:2 withShadow:YES withOpacity:0.1];
}
可以实现一些在布局已定,修改小的地方。
本文介绍了一种在iOS开发中为UIView设置圆角和阴影效果的方法,通过扩展UIView类并自定义layoutSubviews方法实现。
1569

被折叠的 条评论
为什么被折叠?



