如果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
却是指视图的图层上的子图层,如果超出父图层的部分就截取掉
测试图如下:
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;
}
网友评论