美文网首页iOS 知识大全
切圆角、圆角与阴影同时存在总结

切圆角、圆角与阴影同时存在总结

作者: 大码猴 | 来源:发表于2019-11-23 13:46 被阅读0次

    一、切圆角

    1.使用layer. cornerRadius

     view.layer.cornerRadius = 10;
     view.layer.masksToBounds = YES;
    

    2.贝塞尔曲线切

    - (void)layoutSubviews{
       [super layoutSubviews];
       UIRectCorner rec = UIRectCornerTopLeft | UIRectCornerTopRight   UIRectCornerBottomLeft | UIRectCornerBottomRight;
       UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rec cornerRadii:CGSizeMake(15, 15)];
       CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
       maskLayer.frame = self.bounds;
       maskLayer.path = maskPath.CGPath;
       self.layer.mask = maskLayer;
       [self layoutIfNeeded];
    }
    

    二、圆角与阴影共存

    1.设置阴影时圆角masksToBounds = NO,view内部做圆角切割

    UIView *view= [[UIViewalloc] init];
    view.backgroundColor = UIColor.whiteColor;
    view.layer.cornerRadius = 10;
    // bgView.layer.masksToBounds = YES;
    view.layer.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.05].CGColor;
    view.layer.shadowOffset=CGSizeMake(0,5);
    view.layer.shadowOpacity = 1;
    view.layer.cornerRadius = 10;
    

    2.视图底部添加layer

    - (UIView *)shadowView
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        view.backgroundColor = UIColor.whiteColor;
        view.layer.shadowOffset = CGSizeMake(0, 0);
        view.layer.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.05].CGColor;
        view.layer.shadowOpacity = 1;
    
        CALayer *sublayer =[CALayer layer];
        sublayer.frame = view.frame;
        sublayer.backgroundColor = UIColor.lightGrayColor.CGColor;
        sublayer.cornerRadius = 2;
        sublayer.masksToBounds = YES;
        [view.layer addSublayer:sublayer];
        return view;
    }
    

    3.视图底部添加view

    - (void)shadowView
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        view.layer.cornerRadius = 10;
        view.layer.masksToBounds = YES;
    
        UIView *layerView = [[UIView alloc] initWithFrame:view.frame];
        layerView.layer.shadowOffset = CGSizeMake(0, 0);
        layerView.layer.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.05].CGColor;
        layerView.layer.shadowOpacity = 0.5f;
    
        [self addSubview:layerView];
        [selfaddSubview:view];
    }
    

    4.贝塞尔曲线结合layer阴影

    在父视图的drawRect方法上增加创建UIBezierPath,并设置圆角属性,设置完成后赋值给指定圆角和阴影的视图shadowPath

    - (void)drawRect:(CGRect)rect {
        UIBezierPath *recPath = [UIBezierPath bezierPathWithRoundedRect:self.view. cornerRadius:10];
        self.view.layer.shadowPath = recPath.CGPath;
    }
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            self.view = [[UIView alloc] initWithFrame:CGRectMake(0 , 0, 50, 100)];
            self.view.layer.shadowOpacity = 1;
            self.view.layer.shadowColor = UIColor.redColor.CGColor;
            self.view.layer.shadowOffset = CGSizeMake(0, 0);
            [self self.view];
        }
        return self;
    }
    

    注:如果是SDAutoLayout或者Masonry 设置的布局,在layoutIfNeeded之后在设置CALayer

    相关文章

      网友评论

        本文标题:切圆角、圆角与阴影同时存在总结

        本文链接:https://www.haomeiwen.com/subject/aszcwctx.html