1. 简单阴影
我们给layer设置了shadowOpacity后就能得到一个简单的阴影
view.layer.shadowOpacity = 1;
shadowOpacity设置了阴影的不透明度,取值范围在0~1
这里shadow有一个默认值
shadowOffset = CGSizeMake(0, -3)
shadowRadius = 3.0
注意:如果view没有设置背景色阴影也是不会显示的
2. 阴影属性
layer中与阴影相关的属性有以下几个
(CGColorRef*) shadowColor//阴影颜色
(float) shadowOpacity//阴影透明度
(CGSize) shadowOffset//阴影偏移量
(CGFloat) shadowRadius//模糊计算的半径
(CGPathRef*) shadowPath//阴影路径
3. shadowColor 阴影颜色
- (void)AddLaterShadowColor {
self.layer.shadowOpacity = 1;
self.layer.shadowColor = [UIColor redColor].CGColor;
self.layer.shadowColor = [UIColor blueColor].CGColor;
self.layer.shadowColor = [UIColor yellowColor].CGColor;
}
4. shadowOpacity 透明度
- (void)AddLaterShadowOpacity {
self.layer.shadowOpacity = 1;
self.layer.shadowColor = [UIColor whiteColor].CGColor;
self.layer.shadowOpacity = 0.2;
self.layer.shadowOpacity = 0.6;
self.layer.shadowOpacity = 0.9;
}
5. shadowOffset. 偏移量
- (void)AddLaterShadowOpacity {
self.layer.shadowOpacity = 1;
self.layer.shadowColor = [UIColor whiteColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0,0);
self.layer.shadowOffset = CGSizeMake(5,0);
self.layer.shadowOffset = CGSizeMake(-5,0);
self.layer.shadowOffset = CGSizeMake(0,5);
self.layer.shadowOffset = CGSizeMake(0,-5);
self.layer.shadowOffset = CGSizeMake(5,5);
self.layer.shadowOffset = CGSizeMake(-5,-5);
}
6. shadowRadius
shadowRadius其实可以理解为阴影的宽度
- (void)AddLayerShadowRadius {
self.layer.shadowOpacity = 1;
self.layer.shadowColor = [UIColor whiteColor].CGColor;
self.layer.shadowRadius = 0;
self.layer.shadowRadius = 3;
self.layer.shadowRadius = 10;
}
7. shadowPath
- (void)p_setupSubViews {
self.view.backgroundColor = [UIColorwhiteColor];
[selfp_setupViewWithY1:100];
[selfp_setupViewWithY2:170];
[selfp_setupViewWithY3:240];//贝塞尔曲线未闭合
}
- (void)p_setupViewWithY1:(CGFloat)y {
UIView*view = [[UIViewalloc] initWithFrame:CGRectMake(30, y, [UIScreenmainScreen].bounds.size.width -60,50)]; view.backgroundColor = [UIColorwhiteColor];
[self.view addSubview:view];
view.layer.shadowOpacity =1;
UIBezierPath*path = [UIBezierPathbezierPathWithRect:view.bounds];
view.layer.shadowPath = path.CGPath;
}
- (void)p_setupViewWithY2:(CGFloat)y {
UIView*view = [[UIViewalloc] initWithFrame:CGRectMake(30, y, [UIScreenmainScreen].bounds.size.width -60,50)]; view.backgroundColor = [UIColorwhiteColor];
[self.view addSubview:view];
view.layer.shadowOpacity =1;
UIBezierPath*path = [UIBezierPathbezierPath];
[path moveToPoint:CGPointMake(0,0)];
[path addLineToPoint:CGPointMake(0, view.frame.size.height +10)];
[path addLineToPoint:CGPointMake(view.frame.size.width, view.frame.size.height +10)];
[path addLineToPoint:CGPointMake(view.frame.size.width,0)];
[path addLineToPoint:CGPointMake(0,0)];
view.layer.shadowPath = path.CGPath;
}
- (void)p_setupViewWithY3:(CGFloat)y {
UIView*view = [[UIViewalloc] initWithFrame:CGRectMake(30, y, [UIScreenmainScreen].bounds.size.width -60,50)]; view.backgroundColor = [UIColorwhiteColor];
[self.view addSubview:view];
view.layer.shadowOpacity =1;
view.layer.shadowOffset =CGSizeMake(0,0);
UIBezierPath*path = [UIBezierPathbezierPath];
[path moveToPoint:CGPointMake(-5,0)];
[path addLineToPoint:CGPointMake(-5, view.frame.size.height)];
[path addLineToPoint:CGPointMake(view.frame.size.width, view.frame.size.height)];
[path addLineToPoint:CGPointMake(view.frame.size.width,0)];
view.layer.shadowPath = path.CGPath;
}
shadow path
当用bounds设置path时,看起来的效果与只设置了shadowOpacity一样
但是添加了shadowPath后消除了离屏渲染问题
注意:
使用阴影的时候 self.layer.masksToBounds 不能=YES,否则阴影部分会截掉
UIImageView 添加阴影的时候是加在UIImageView.image上,会出现阴影渲染在image四周,在UIImageView里