通常方式,设置view的layer属性
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(50,50, 200, 300)]; v.backgroundColor = [UIColor blueColor]; v.layer.shadowColor = [UIColor redColor].CGColor; //opaque black v.layer.shadowRadius = 0; //3 v.layer.shadowOffset = CGSizeMake(20, 50); v.layer.shadowOpacity = 0.1; //0
shadowColor:阴影颜色,默认黑色
shadowOpacity:颜色的透明度,默认为0
shadowOffset:阴影的便宜量,(x,y)
shadowRadius:阴影半径,默认值3,等于0时实际效果表现为阴影呈一条直线。
有时候需要自定义的在某些边上加阴影效果,此时只使用shawdowoffset显然不能满足需求,因此我们使用shadowpath属性可以解决。
当shawdowoffset和shadowpath同时存在时,两者值都生效,shawdowoffset偏移的参照点由(0,0)变成shadowpath的x、y坐标。
1、只在某一边加阴影
v.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, -10, 200, 20)].CGPath; //高度保证阴影能无缝衔接view顶部
2、四周同时有阴影
v.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(-10, -10, 220, 320)].CGPath;
3、左右两边阴影
v.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(-10, 0, 220, 300)].CGPath;