如果UIImageView、只添加陰影不設(shè)置圓角可使用:介紹下加陰影幾個(gè)屬性的概念
imageView.layer.shadowColor = [UIColor blackColor].CGColor;
// 設(shè)置陰影偏移量
imageView.layer.shadowOffset = CGSizeMake(0,0);
// 設(shè)置陰影透明度
imageView.layer.shadowOpacity = 1;
// 設(shè)置陰影半徑
imageView.layer.shadowRadius = 5;
imageView.clipsToBounds = NO;
介紹兩個(gè)概念:
clipsToBounds
是指視圖上的子視圖,如果超出父視圖的部分就截取掉
masksToBounds
卻是指視圖的圖層上的子圖層,如果超出父圖層的部分就截取掉
測試圖如下:
示例代碼
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;
}