layer中与阴影相关的属性有以下几个
float shadowOpacity; //阴影透明度
CGSize shadowOffset; //阴影偏移量
CGFloat shadowRadius; //模糊计算的半径
CGColorRef shadowColor; //阴影颜色
CGPathRef shadowPath; //阴影路径
- shadowOpacity
设置了阴影的不透明度,取值范围在0~1,默认值0
- shadowOffset
设置了阴影的偏移量,默认值CGSizeMake(0, -3)
- shadowRadius
设置了阴影模糊计算的半径,可以理解为阴影的宽度,默认值3.0
- shadowColor
设置了阴影的颜色,默认值为黑色RGBA(0,0,0,1)
- shadowPath
设置了阴影的路径
如果UIView没有设置背景色,则阴影无法正常显示。
示例1:
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
backView.backgroundColor = [UIColor greenColor];
backView.layer.shadowOpacity = 0.5;
backView.layer.shadowOffset = CGSizeMake(5, 5);
[self.view addSubview:backView];
示例2:
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
backView.backgroundColor = [UIColor greenColor];
backView.layer.shadowOpacity = 0.5;
backView.layer.shadowRadius = 15;
[self.view addSubview:backView];
示例3:
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
backView.backgroundColor = [UIColor greenColor];
backView.layer.cornerRadius = 5;
backView.layer.shadowOpacity = 0.5;
backView.layer.shadowOffset = CGSizeMake(5, 5);
[self.view addSubview:backView];