How to draw a smooth circle with CAShapeLayer and UIBezierPath?

前端 未结 5 1499
闹比i
闹比i 2020-12-07 07:14

I am attempting to draw a stroked circle by using a CAShapeLayer and setting a circular path on it. However, this method is consistently less accurate when rendered to the

5条回答
  •  粉色の甜心
    2020-12-07 08:06

    Use this method to draw UIBezierPath

    /*********draw circle where double tapped******/
    - (UIBezierPath *)makeCircleAtLocation:(CGPoint)location radius:(CGFloat)radius
    {
        self.circleCenter = location;
        self.circleRadius = radius;
    
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path addArcWithCenter:self.circleCenter
                        radius:self.circleRadius
                    startAngle:0.0
                      endAngle:M_PI * 2.0
                     clockwise:YES];
    
        return path;
    }
    

    And draw like this

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = [[self makeCircleAtLocation:location radius:50.0] CGPath];
        shapeLayer.strokeColor = [[UIColor whiteColor] CGColor];
        shapeLayer.fillColor = nil;
        shapeLayer.lineWidth = 3.0;
    
        // Add CAShapeLayer to our view
    
        [gesture.view.layer addSublayer:shapeLayer];
    

提交回复
热议问题