iOS view添加阴影
Posted 火海夕
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS view添加阴影相关的知识,希望对你有一定的参考价值。
四边阴影
UIView* view=[UIView new];
view.layer.shadowColor = [UIColor lightGrayColor].CGColor;
view.layer.shadowOffset = CGSizeMake(0,0.5);
view.layer.shadowOpacity = 0.6;
view.layer.shadowRadius = 1.0;
view.clipsToBounds = NO; ``
上边阴影
- (void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor
theView.layer.shadowColor = theColor.CGColor;
theView.layer.shadowOffset = CGSizeMake(0,0);
theView.layer.shadowOpacity = 0.5;
theView.layer.shadowRadius = 5;
// 单边阴影 顶边
float shadowPathWidth = theView.layer.shadowRadius;
CGRect shadowRect = CGRectMake(0, 0, theView.bounds.size.width, shadowPathWidth);//0-shadowPathWidth/2.0
UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
theView.layer.shadowPath = path.CGPath;
下边阴影
- (void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor
theView.layer.shadowColor = theColor.CGColor;
theView.layer.shadowOffset = CGSizeMake(0,0);
theView.layer.shadowOpacity = 0.5;
theView.layer.shadowRadius = 5;
// 单边阴影 顶边
float shadowPathWidth = theView.layer.shadowRadius;
CGRect shadowRect = CGRectMake(0, 40, theView.bounds.size.width, shadowPathWidth);//0-shadowPathWidth/2.0
UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
theView.layer.shadowPath = path.CGPath;
以此类推,可以设置阴影的方向
注释:控制shadowRect的坐标是控制阴影位置的关键,而设置view.clipsToBounds = NO; 这个属性为no是必须的,不然阴影不会显示
以上是关于iOS view添加阴影的主要内容,如果未能解决你的问题,请参考以下文章