让带有圆角的空间有阴影,有两种方法
方法一:用一个透明的view作为这个圆角控件的父控件
Snip20180131_4.png具体就是让父控件的背景颜色为
clearColor
,再设置父控件的阴影颜色。圆角控件只需要切圆角就好了。
self.rectangleView.layer.cornerRadius = 20;
self.rectangleView.layer.masksToBounds = YES;
self.recBGView.backgroundColor = [UIColor clearColor];
self.recBGView.layer.shadowColor = [UIColor redColor].CGColor;
self.recBGView.layer.shadowOffset = CGSizeZero;
self.recBGView.layer.shadowOpacity = 1;
方法二,设置控件的shadowPath
第二种方法要自定义控件,因为路径要在- (void)drawRect:(CGRect)rect
方法里面绘制才有效
- (void)drawRect:(CGRect)rect {
// Drawing code
// 这个阴影路径可以自己画,灵活很多
UIBezierPath *recPath = [UIBezierPath bezierPathWithRoundedRect:self.recView.bounds cornerRadius:50];
self.recView.layer.shadowPath = recPath.CGPath;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
UIView *recView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 50, 30, 100, 100)];
recView.backgroundColor = [UIColor whiteColor];
recView.layer.shadowOpacity = 1;
recView.layer.shadowColor = [UIColor redColor].CGColor;
recView.layer.cornerRadius = 50;
[self addSubview:recView];
self.recView = recView;
}
return self;
}
网友评论