iOS 进阶

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

2020-05-10  本文已影响0人  SmileYang966

展示效果

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

上一篇下一篇

猜你喜欢

热点阅读