简单方法画带圆角的阴影背景

2016-06-24  本文已影响208人  Kadima_章鱼

按一般画阴影方法,这样便可以画出一个阴影背景

UIImageView *imageView = [[UIImageView alloc] init];
imageView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色  
imageView.layer.shadowOffset = CGSizeMake(4,4);//shadowOffset阴影偏移,x向右偏移4,y向下偏移4,默认(0, -3),这个跟shadowRadius配合使用  
imageView.layer.shadowOpacity = 0.8;//阴影透明度,默认0  
imageView.layer.shadowRadius = 4;//阴影半径,默认3 

加上圆角

imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = imageViewWH/2;

因为masksToBounds表示对frame外的内容进行裁剪,只可显示frame内的内容,由于阴影是在frame外,所以直接被裁剪了。

这里我们可以用如下方法简单实现带圆角阴影

我们可以把带圆角的imageView放到一个和它大小的UIView中,让这个view来实现阴影

CGFloat headerWH = 80;
UIView *shadow = [[UIView alloc] init];
shadow.backgroundColor = [UIColor clearColor];//这里一定要clearColor,如果view有颜色则画出来的阴影跟随view下面,clearColor画出来的阴影则是跟随加在view上的带圆角子视图的
shadow.layer.shadowColor = [UIColor lightGrayColor].CGColor;
shadow.layer.shadowRadius = 4;
shadow.layer.shadowOffset = CGSizeMake(4, 4);
shadow.layer.shadowOpacity = 1;
shadow.clipsToBounds = NO;
shadow.frame = CGRectMake(100, 100, headerWH, headerWH);
[self addSubview:shadow];
        
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@""];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = headerWH/2;
imageView.frame = CGRectMake(0, 0, headerWH, headerWH);
[shadow addSubview:imageView];

上一篇下一篇

猜你喜欢

热点阅读