iOS view添加指定切角

2023-07-30  本文已影响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;
    }
}
 
上一篇 下一篇

猜你喜欢

热点阅读