bezierPathWithRoundedRect不是完美的圆 [英] bezierPathWithRoundedRect not perfect circle

查看:1190
本文介绍了bezierPathWithRoundedRect不是完美的圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对绘制圆有一些问题:



此简单的代码演示了:

 -(void)drawRect:(CGRect)rect {

self.layer.sublayers = nil;
CGFloat半径= self.frame.size.width / 2;

个圆= [CAShapeLayer图层];
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; //动画超过10秒左右。
drawAnimation.repeatCount = 1.0; //仅动画一次。.

drawAnimation.fromValue = [NSNumber numberWithFloat:0];
drawAnimation.toValue = [NSNumber numberWithFloat:1];

drawAnimation.removedOnCompletion =否;
drawAnimation.fillMode = kCAFillModeForwards;

drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

drawAnimation.delegate =自我;
[circle addAnimation:drawAnimation forKey:@ drawCircleAnimation];

}

我们得到结果




前三个圆圈通过上述方法绘制,最后一个我在PS中画了一个圆圈。
如果您可以看到bezierPathWithRoundedRect绘制半径不正确的圆,我认为(某些圆角已添加到圆中)。 PS:我需要为项目使用bezierPathWithArcCenter,但是当我重用bezierPathWithRoundedRect时,我也遇到了同样的问题!

解决方案

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



根据定义,RoundedRect是方形的。


I have some problem with draw circle:

This simply code demonstrates that:

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

}

And we have result

First three circles draws by method above, last circle I draw in PS. If u can see bezierPathWithRoundedRect draws not perfect circle with right radius I think (some corners added to circle). How to draw circle like last circle in a picture?

PS: I need to use bezierPathWithArcCenter for my project, but when I reuse bezierPathWithRoundedRect I has same problem!

解决方案

You need to use + (NSBezierPath *)bezierPathWithOvalInRect:(NSRect)aRect for a circle.

By definition a RoundedRect is squarish.

这篇关于bezierPathWithRoundedRect不是完美的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆