bezierPathWithRoundedRect不是完美的圈子

我画圈有一些问题:

这个简单的代码表明:

- (void)drawRect:(CGRect)rect { self.layer.sublayers = nil; CGFloat radius = self.frame.size.width/2; circle = [CAShapeLayer layer]; circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.width) cornerRadius:radius].CGPath; circle.fillColor = [UIColor clearColor].CGColor; circle.strokeColor = [UIColor redColor].CGColor; circle.lineWidth = 4; [self.layer addSublayer:circle]; CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; drawAnimation.duration = 1.0; // "animate over 10 seconds or so.." drawAnimation.repeatCount = 1.0; // Animate only once.. drawAnimation.fromValue = [NSNumber numberWithFloat:0]; drawAnimation.toValue = [NSNumber numberWithFloat:1]; drawAnimation.removedOnCompletion = NO; drawAnimation.fillMode = kCAFillModeForwards; drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; drawAnimation.delegate = self; [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"]; } 

我们有结果

在这里输入图像说明 前三圈用上面的方法绘制,上一圈我在PS中画出。 如果你能看到bezierPathWithRoundedRect用正确的半径画出不完美的圆圈,我想(有些angular落加上了圆圈)。 如何在图片中绘制最后一个圆的圆圈?

PS:我需要为我的项目使用bezierPathWithArcCenter,但是当我重新使用bezierPathWithRoundedRect时,我遇到了同样的问题!

您需要使用+ (NSBezierPath *)bezierPathWithOvalInRect:(NSRect)aRect为一个圆。

按照定义,RoundedRect是方形的。

要绘制圆形,您需要使用弧线。

看看appendBezierPathWithArcFromPoint:toPoint:radius:

椭圆形不是椭圆形 (尽pipe它们通常用英文交替使用)。 椭圆是一个泛化圆。

我发现最好的结果是在drawRect:使用CGContextAddEllipseInRect: drawRect:

 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGRect boundingRect = CGRectMake(2.0f, 2.0f, CGRectGetWidth(self.bounds)-4.0, CGRectGetHeight(self.bounds)-4.0); CGContextSetLineWidth(context, 3.0); CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); CGContextBeginPath(context); CGContextAddEllipseInRect(context, boundingRect); CGContextDrawPath(context, kCGPathFillStroke); UIGraphicsEndImageContext(); }