ios 阴影与部分圆角并存

圆角和阴影并存

首先先说阴影和view的全部圆角并存

不考虑性能的情况下

1、在view下面在加一个view设置阴影
ios11.0以下

UIView *bgView = [UIView new];
bgView.backgroundColor = [UIColor colorWithHex:CenColor];
[self addSubview:bgView];
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
    (void)make.edges;
}];
bgView.layer.cornerRadius  = 5;
bgView.layer.masksToBounds = YES;

CGFloat coner = self.bounds.size.height/2.0;
bgView.layer.shadowColor = [UIColor colorWithHex:0x000000].CGColor;
bgView.layer.shadowOffset =CGSizeMake(1, 2);
bgView.layer.shadowOpacity = 0.15;
bgView.layer.shadowRadius =4.0;
bgView.clipsToBounds =NO;
bgView.layer.cornerRadius = coner;
UIView *conerView = [UIView new];
conerView.backgroundColor = [UIColor colorWithHex:CenColor];
[self addSubview:conerView];
[conerView mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.edges;
}];

2、加入一个layer显示阴影
转载--https://blog.csdn.net/qcx321/article/details/78021138

// 阴影
CALayer *subLayer=[CALayer layer];
CGRect fixframe = imgView.frame;
subLayer.frame= fixframe;
subLayer.cornerRadius=8;
subLayer.backgroundColor=[[UIColor blackColor]       colorWithAlphaComponent:0.8].CGColor;
subLayer.masksToBounds=NO;
subLayer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
subLayer.shadowOffset = CGSizeMake(3,2);//shadowOffset阴影偏移,x向右偏移3,y向下偏移2,默认(0, -3),这个跟shadowRadius配合使用
subLayer.shadowOpacity = 0.8;//阴影透明度,默认0
subLayer.shadowRadius = 4;//阴影半径,默认3
[self.view.layer insertSublayer:subLayer below:imgView.layer];

那么有阴影并且只切部分圆角要怎么做呢
首先一个view

UIView *bgView = [UIView new];
bgView.backgroundColor = [UIColor colorWithHex:CenColor];
[self addSubview:bgView];
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
    (void)make.edges;
}];
//阴影
CGFloat coner = self.bounds.size.height/2.0;
bgView.layer.shadowColor = [UIColor colorWithHex:0x000000].CGColor;
bgView.layer.shadowOffset =CGSizeMake(1, 2);
bgView.layer.shadowOpacity = 0.15;
bgView.layer.shadowRadius =4.0;
bgView.clipsToBounds =NO;
bgView.layer.cornerRadius = coner;

在ios11.0以上还是非常方便的

if (@available(iOS 11.0, *)) {
    bgView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMinXMaxYCorner;
}

ios11.0以下就需要用到UIBezierPath了,在bgView的上面再增加一个view用来切部分圆角

    UIView *conerView = [UIView new];
    conerView.backgroundColor = [UIColor colorWithHex:CenColor];
    [self addSubview:conerView];
    [conerView mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.edges;
    }];
    lastView = conerView;
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(coner, coner)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = path.CGPath;
    maskLayer.masksToBounds = YES;
    conerView.layer.mask = maskLayer;

你可能感兴趣的:(ios 阴影与部分圆角并存)