美文网首页iOS 进阶
iOS-添加带圆角的矩形阴影

iOS-添加带圆角的矩形阴影

作者: SmileYang966 | 来源:发表于2020-05-10 21:02 被阅读0次

展示效果

shadowView.png

设计思路

  1. 先创建一个shadowView,负责阴影的显示,设置其shadowColor、shadowOffset、shadowRadius以及shadowOpacity的设置
  2. 再创建一个圆角view,负责view圆角的展示。设置圆角的相关设置,例如cornerRadius以及maskToBounds
  3. 将圆角view添加到shadowView上

贴代码

-(void)shadowViewWithCorner{
    //1.先创建一个shadowView(阴影层view),负责shadow的显示
    UIView *shadowView = [[UIView alloc]initWithFrame:CGRectMake(100,100,200,200)];
    [self.view addSubview:shadowView];
    shadowView.layer.shadowColor = UIColor.lightGrayColor.CGColor;
    shadowView.layer.shadowOffset = CGSizeMake(0,0);
    shadowView.layer.shadowRadius = 10.0f;
    shadowView.layer.shadowOpacity = 10.0f;
    
    //2.再创建一个roundCornerView(圆角view),负责圆角的展示
    UIView *roundCornerView = [[UIView alloc]initWithFrame:CGRectMake(0,0,200,200)];
    roundCornerView.layer.cornerRadius = 10.0f;
    roundCornerView.layer.masksToBounds = YES;
    roundCornerView.backgroundColor = UIColor.whiteColor;

    //3.将圆角view添加到shadowView上
    [shadowView addSubview:roundCornerView];
}

更好的解决方案

第三方框架YCShadowView

相关文章

网友评论

    本文标题:iOS-添加带圆角的矩形阴影

    本文链接:https://www.haomeiwen.com/subject/ybujnhtx.html