iOS-给UIView,UIImageView,UIlabel设置阴影效果

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

如果UIImageView,只添加阴影不设置圆角可使用:介绍下加阴影几个属性的概念

    imageView.layer.shadowColor = [UIColor blackColor].CGColor;
    // 设置阴影偏移量
    imageView.layer.shadowOffset = CGSizeMake(0,0);
    // 设置阴影透明度
    imageView.layer.shadowOpacity = 1;
    // 设置阴影半径
    imageView.layer.shadowRadius = 5;
    imageView.clipsToBounds = NO;

介绍两个概念:

clipsToBounds
是指视图上的子视图,如果超出父视图的部分就截取掉
masksToBounds
却是指视图的图层上的子图层,如果超出父图层的部分就截取掉

测试图如下:

iOS-给UIView,UIImageView,UIlabel设置阴影效果
VIew加阴影.png

示例代码

UIView加阴影:可直接加阴影

- (void)createViewShadow {
    
    CGFloat margin = ([UIScreen mainScreen].bounds.size.width - width * 3 - leftMargin * 2)/2.f;
    UIView *view1 = [[UIView alloc]init];
    view1.frame = CGRectMake(leftMargin, width + leftMargin*1.5, width, width);
    [self.view addSubview:view1];
    self.view1 = view1;
    view1.backgroundColor = [UIColor yellowColor];
    view1.layer.shadowColor = [UIColor blackColor].CGColor;
    view1.layer.shadowOffset = CGSizeMake(-shadowRadius,-shadowRadius);
    view1.layer.shadowOpacity = 1;
    view1.layer.shadowRadius = shadowRadius;
    view1.layer.cornerRadius = shadowRadius;
    view1.layer.masksToBounds = YES;
    view1.clipsToBounds = NO;
    
    UIView *view2 = [[UIView alloc]init];
    view2.frame = CGRectMake(leftMargin + (width + margin), width + leftMargin*1.5, width, width);
    [self.view addSubview:view2];
    self.view2 = view2;
    view2.backgroundColor = [UIColor yellowColor];
    view2.layer.shadowColor = [UIColor blackColor].CGColor;
    view2.layer.shadowOffset = CGSizeMake(0,0);
    view2.layer.shadowOpacity = 1;
    view2.layer.shadowRadius = shadowRadius;
    view2.layer.cornerRadius = shadowRadius;
    view2.layer.masksToBounds = YES;
    view2.clipsToBounds = NO;
    
    UIView *view3 = [[UIView alloc]init];
    view3.frame = CGRectMake(leftMargin + (width + margin)*2, width + leftMargin*1.5, width, width);
    [self.view addSubview:view3];
    self.view3 = view3;
    view3.backgroundColor = [UIColor yellowColor];
    view3.layer.shadowColor = [UIColor blackColor].CGColor;
    view3.layer.shadowOffset = CGSizeMake(shadowRadius,shadowRadius);
    view3.layer.shadowOpacity = 1;
    view3.layer.shadowRadius = shadowRadius;
    view3.layer.cornerRadius = shadowRadius;
    view3.layer.masksToBounds = YES;
    view3.clipsToBounds = NO;
}

UIImageView加阴影:需要加背景View加阴影、imageView裁圆角

- (void)createImageViewShadow {
    
    UIView *bgView = [[UIView alloc]init];
    bgView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - width)/2.f, (width + leftMargin*1.5)*2, width, width);
    [self.view addSubview:bgView];
    self.bgView1 = bgView;
    bgView.backgroundColor = [UIColor yellowColor];
    bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    bgView.layer.shadowOffset = CGSizeMake(0,0);
    bgView.layer.shadowOpacity = 1;
    bgView.layer.shadowRadius = shadowRadius;
    bgView.layer.cornerRadius = shadowRadius;
    bgView.layer.masksToBounds = YES;
    bgView.clipsToBounds = NO;
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - width)/2.f, (width + leftMargin*1.5 )*2, width, width);
    [self.view addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"shadow.jpg"];
    imageView.layer.cornerRadius = shadowRadius;
    imageView.layer.masksToBounds = YES;
}

UILabel加阴影:需要加背景View加阴影、label裁圆角

- (void)createLabelShadow {
    
    UIView *bgView = [[UIView alloc]init];
    bgView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - width)/2.f, (width + leftMargin*1.5)*3, width, width);
    [self.view addSubview:bgView];
    self.bgView2 = bgView;
    bgView.backgroundColor = [UIColor yellowColor];
    bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    bgView.layer.shadowOffset = CGSizeMake(0,0);
    bgView.layer.shadowOpacity = 1;
    bgView.layer.shadowRadius = shadowRadius;
    bgView.layer.cornerRadius = shadowRadius;
    bgView.layer.masksToBounds = YES;
    bgView.clipsToBounds = NO;
    
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - width)/2.f, (width + leftMargin*1.5)*3, width, width);
    [self.view addSubview:label];
    label.backgroundColor = [UIColor redColor];
    label.layer.cornerRadius = shadowRadius;
    label.layer.masksToBounds = YES;
}

原始码下载(在代码给view设置阴影效果部分)

 

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术Dog|博客
原文地址:《iOS-给UIView,UIImageView,UIlabel设置阴影效果》 发布于2019-10-12

分享到:
赞(0)

评论 抢沙发

9 + 6 =


iOS-给UIView,UIImageView,UIlabel设置阴影效果

长按图片转发给朋友

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录