美文网首页
iOS view添加指定切角

iOS view添加指定切角

作者: 简繁之间_来去自然 | 来源:发表于2023-07-30 09:49 被阅读0次
    // 指定顺序左上-右上-右下-左下
    - (void)addCorner:(UIView *)view radiusArr:(NSArray *)radiusArr {
        CGFloat w = view.frame.size.width;
        CGFloat h = view.frame.size.height;
        if (radiusArr.count == 4) {
            CGFloat leftTop = [radiusArr[0] floatValue];
            CGFloat rightTop = [radiusArr[1] floatValue];
            CGFloat rightBtm = [radiusArr[2] floatValue];
            CGFloat leftBtm = [radiusArr[3] floatValue];
    
            UIBezierPath *path = [UIBezierPath bezierPath];
    
            [path moveToPoint:CGPointMake(0, leftTop)];
            [path addArcWithCenter:CGPointMake(leftTop, leftTop) radius:leftTop startAngle:-M_PI endAngle:- M_PI / 2 clockwise:true];
            [path addLineToPoint:CGPointMake(w - rightTop, 0)];
            [path addArcWithCenter:CGPointMake(w - rightTop, rightTop) radius:rightTop startAngle:- M_PI / 2 endAngle:0 clockwise:true];
            [path addLineToPoint:CGPointMake(w , h - rightBtm)];
            [path addArcWithCenter:CGPointMake(w - rightBtm, h - rightBtm) radius:rightBtm startAngle:0 endAngle: M_PI_2 clockwise:true];
            [path addLineToPoint:CGPointMake(leftBtm, h)];
            [path addArcWithCenter:CGPointMake(leftBtm, h - leftBtm) radius:leftBtm startAngle:M_PI_2 endAngle: M_PI clockwise:true];
            [path addLineToPoint:CGPointMake(0, leftTop)];
            [path closePath];
    
            CAShapeLayer * shaperLayer = [[CAShapeLayer alloc] init];
            shaperLayer.path = path.CGPath;
            shaperLayer.frame = CGRectMake(0, 0, w, h);
            view.layer.mask = shaperLayer;
        }
    }
     
    

    相关文章

      网友评论

          本文标题:iOS view添加指定切角

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