iOS 视图添加阴影
2020-10-14 本文已影响0人
三岁就很乖
1、四边加阴影
四边加阴影
2、顶部加阴影
顶部加阴影
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIView *floatView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
floatView.backgroundColor = [UIColor systemPinkColor];
[self.view addSubview:floatView];
// [self addFourSidesShadowToView:floatView withColor:[UIColor blackColor]];
[self addSingleSidesShadowToView:floatView withColor:[UIColor blackColor]];
}
//添加四边阴影效果
-(void)addFourSidesShadowToView:(UIView *)theView withColor:(UIColor*)theColor{
//阴影颜色
theView.layer.shadowColor = theColor.CGColor;
//阴影偏移
theView.layer.shadowOffset = CGSizeMake(0, 0 );
//阴影透明度,默认0
theView.layer.shadowOpacity = 0.3;
//阴影半径,默认3
theView.layer.shadowRadius = 8;
}
//在顶部添加阴影
-(void)addSingleSidesShadowToView:(UIView *)theView withColor:(UIColor*)theColor{
//阴影颜色
theView.layer.shadowColor = theColor.CGColor;
//阴影偏移
theView.layer.shadowOffset = CGSizeMake(0, 0 );
//阴影透明度,默认0
theView.layer.shadowOpacity = 0.3;
//阴影半径,默认3
theView.layer.shadowRadius = 8;
//单边阴影
CGFloat shadowPathWidth = theView.layer.shadowRadius;
CGRect shadowRect = CGRectMake(0, 0-shadowPathWidth/2, theView.bounds.size.width, shadowPathWidth);
UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
theView.layer.shadowPath = path.CGPath;
}