画圈有问题:
这段简单的代码演示了:
- (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 是方形的。
关于ios - bezierPathWithRoundedRect 不是完美的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063680/