iOS 视图添加阴影

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;
}

来源:https://www.jianshu.com/p/d299ddf5aaaf

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容