bezierPathWithRoundedRect

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绘制半径不正确的圆形,我认为(某些圆角已添加到圆中)。如何在图片中画出像最后一个圆圈一样的圆圈?

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

最佳答案

您需要使用+ (NSBezierPath *)bezierPathWithOvalInRect:(NSRect)aRect圈。

根据定义,RoundedRect是方形的。

10-04 20:45