iOS:如何用NSTimer逐步绘制一个圆圈

我想一步一步(如animation计时器)绘制一个没有填充(仅圈的边框)的圆圈。 1旋转等于1天(24小时)。 我真的不知道该怎么做。

我做的步骤

1)我试过https://github.com/danielamitay/DACircularProgress (这是太宽的进展路线)

2)我试图绘制一个有许多弧的圆。

你可以给我一些代码吗? 我真的很困惑。 提前致谢。

编辑

我想使用NSTimer,因为我有一个允许用户停止画圆的button。 如果用户再次触摸一个button – 绘图将不得不继续。

我会做的是创build一个圆圈的path,并使用CAShapeLayer的animation和strokeEndanimation,类似于我在这个答案中做的 。

它看起来像这样(但我没有运行这个代码,所以可能会有拼写错误和其他错误):

UIBezierPath *circle = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2.0*M_PI clockwise:YES]; CAShapeLayer *circleLayer = [CAShapeLayer layer]; circleLayer.bounds = CGRectMake(0, 0, 2.0*radius, 2.0*radius); circleLayer.path = circle.CGPath; circleLayer.strokeColor = [UIColor orangeColor].CGColor; circleLayer.lineWidth = 3.0; // your line width CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; drawAnimation.duration = 10.0; // your duration // Animate from no part of the stroke being drawn to the entire stroke being drawn drawAnimation.fromValue = @0; drawAnimation.toValue = @1; 

请注意,path和形状图层都有一个位置,所以应该相对于形状图层框架的原点来定义圆形path。 首先定义形状图层,然后在其边界内创build一个椭圆形可能会更清晰:

 CAShapeLayer *circleLayer = [CAShapeLayer layer]; circleLayer.bounds = CGRectMake(0, 0, 2.0*radius, 2.0*radius); circleLayer.position = center; // Set center of the circle // Create a circle inside of the shape layers bounds UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:circleLayer.bounds]; circleLayer.path = circle.CGPath; // Same appearance configuration as before circleLayer.strokeColor = [UIColor orangeColor].CGColor; circleLayer.lineWidth = 3.0; // your line width 

如果DACircleProgress 在这里input链接描述,否则它适用于你,它看起来像你可以很容易地设置线的厚度。

与具有简单的lineWidthtypes属性相反,该库的作者似乎根据与圆的半径的比例来设置厚度。 这存在为该类的thicknessRatio属性。 例如,如果您的半径是40,那么将thicknessRatio设置为0.025应该产生1的线宽。该库似乎很简单并且深思熟虑 – 考虑使用它或者从中学习。

默认设置为0.3,所以半径为40的圆的进度线厚度为12.这可能是你所看到的。

祝你好运!