UIBezierPath Triangle with rounded edges

前端 未结 6 1457
耶瑟儿~
耶瑟儿~ 2020-11-30 18:00

I have designed this code to generate a Bezier Path that is used as a path for a CAShapeLayer to mask a UIView (the view\'s height and width are variable)

This code

6条回答
  •  一个人的身影
    2020-11-30 18:40

    @David's geometry is cool and educational. But you really don't need to go through the whole geometry this way. I'll offer a much simpler code:

    CGFloat radius = 20;
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, (center.x + bottomLeft.x) / 2, (center.y + bottomLeft.y) / 2);
    CGPathAddArcToPoint(path, NULL, bottomLeft.x, bottomLeft.y, bottomRight.x, bottomRight.y, radius);
    CGPathAddArcToPoint(path, NULL, bottomRight.x, bottomRight.y, center.x, center.y, radius);
    CGPathAddArcToPoint(path, NULL, center.x, center.y, bottomLeft.x, bottomLeft.y, radius);
    CGPathCloseSubpath(path);
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithCGPath:path];
    CGPathRelease(path);
    

    bezierPath is what you need. The key point is that CGPathAddArcToPoint does the geometry for you.

提交回复
热议问题