四边阴影
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是必须的,不然阴影不会显示