ios - bezierPathWithRoundedRect 不是完美的圆

标签 ios objective-c uiview core-graphics geometry

画圈有问题:

这段简单的代码演示了:

- (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"];

}

我们有结果

enter image description here 前三个圆是用上面的方法画的,最后一个圆是我用PS画的。 如果你能看到 bezierPathWithRoundedRect 绘制的不是完美的圆,我认为它的半径是正确的(一些角被添加到圆上)。如何画出像图片最后一个圆圈一样的圆圈?

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

最佳答案

你需要使用 + (NSBezierPath *)bezierPathWithOvalInRect:(NSRect)aRect 来画一个圆。

根据定义,RoundedRect 是方形的。

关于ios - bezierPathWithRoundedRect 不是完美的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063680/

相关文章:

ios - 如何修改PKDrawing中的单个笔画? - PencilKit/SwiftUI

Objective-C 创建一个 C 数组属性

ios - 在动画期间观察 UIView 框架的变化

ios - 从 UIView 子类访问属性

ios - 无法在 (UIButton) 上设置 () 用户定义的检查属性

ios - 如何确保具有重复动画的 UITableView 标题单元格 subview 在 reloadData、用户滚动等之后继续动画?

ios - Swift ios 将多个项目连接到同一个 IBOutlet

c++ - Objective-C 集成 C 库并传递字符串值 - ARC 错误

ios - 在框架内将 Swift 文件公开给 Objective-C

ios - UIView/CALayer : Transform triggers layoutSubviews in superview