【发布时间】:2015-03-15 17:14:59
【问题描述】:
画圈有问题:
这个简单的代码表明:
- (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 时,我遇到了同样的问题!
【问题讨论】:
-
圆角矩形是带圆角的矩形。一个圆圈就是一个圆圈。如果你想要一个圆圈,就画一个圆圈。
-
添加一些上下文。从数学上讲,贝塞尔曲线是参数三次多项式的加权和。它们用途广泛,但它们不会为原始形状提供相同的结果。另外,不要对此投反对票;这是个好问题。
-
@boyfarrell,请检查更新问题。
-
为什么要在这里添加这个?问一个不同的问题。并将其设为已回答。
-
@boyfarrell 你是对的,但不是回答,bezierPathWithOvalInRect = 结果是一样的
标签: ios objective-c uiview core-graphics geometry