UIBezierPath Triangle with rounded edges

前端 未结 6 1433
耶瑟儿~
耶瑟儿~ 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:21

    @an0 - Thank you! I'm brand new, so I don't have the reputation to mark as useful or comment, but you saved me a ton of time today.

    I know it is goes beyond the scope of the question, but the same logic works with CGContextBeginPath(), CGContextMoveToPoint(), CGContextAddArcToPoint(), and CGContextClosePath() in case anyone needs to use that instead.

    If anyone is interested, here's my code for an equilateral triangle pointing right. The triangleFrame is a predefined CGRect and the radius is a predefined CGFloat.

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    
    CGContextMoveToPoint(context, CGRectGetMinX(triangleFrame), CGRectGetMidY(triangleFrame)); CGRectGetMidY(triangleFrame));
    CGContextAddArcToPoint(context, CGRectGetMinX(triangleFrame), CGRectGetMidY(triangleFrame), CGRectGetMinX(triangleFrame), CGRectGetMinY(triangleFrame), radius);
    CGContextAddArcToPoint(context, CGRectGetMinX(triangleFrame), CGRectGetMinY(triangleFrame), CGRectGetMaxX(triangleFrame), CGRectGetMidY(triangleFrame), radius);
    CGContextAddArcToPoint(context, CGRectGetMaxX(triangleFrame), CGRectGetMidY(triangleFrame), CGRectGetMinX(triangleFrame), CGRectGetMaxY(triangleFrame), radius);
    CGContextAddArcToPoint(context, CGRectGetMinX(triangleFrame), CGRectGetMaxY(triangleFrame), CGRectGetMinX(triangleFrame), CGRectGetMidY(triangleFrame), radius);
    

    Of course, each point can be defined more specifically for irregular triangles. You can then CGContextFillPath() or CGContextStrokePath() as desired.

提交回复
热议问题