如何围绕圆圈旋转CAShapeLayer弧?

时间:2015-08-29 16:02:05

标签: ios core-graphics core-animation calayer quartz-core

我有两个CAShapeLayer形成以下显示:

enter image description here

它们是用这段代码创建的:

CALayer *layer = [loadingView layer];
CAShapeLayer *circle = [CAShapeLayer layer];
[circle setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 5, loadingView.bounds.size.width - 10, loadingView.bounds.size.height - 10)] CGPath]];
[circle setStrokeColor:[UIColorFromRGB(0xffffff, .15) CGColor]];
[circle setLineWidth:5];
[circle setFillColor:[[UIColor clearColor] CGColor]];
[layer addSublayer:circle];

CAShapeLayer *arc = [CAShapeLayer layer];
[arc setPath:[[UIBezierPath bezierPathWithArcCenter:CGPointMake(27, 27) radius:22 startAngle:1.25*M_PI endAngle:1.75*M_PI clockwise:YES] CGPath]];
[arc setStrokeColor:[UIColorFromRGB(0xffffff, .8) CGColor]];
[arc setLineWidth:5];
[arc setFillColor:[[UIColor clearColor] CGColor]];
[layer addSublayer:arc];

我希望较小的不透明弧保持其大小并无限旋转。我已尝试应用other answers上发现的一些transform.rotation动画,但是弧线最终会转变为无法预测的方向。

我也尝试以相同的速率提升弧的strokeStartstrokeEnd,这首先得到我想要的效果但由于这些属性的性质而无法循环进行。

就是这样,如何实现这种效果?

2 个答案:

答案 0 :(得分:1)

这里最好的选择是旋转你的整个圈子。围绕其中心旋转loadingView,您将获得所需的效果。你可以获得额外的好处,甚至可以非常容易地修改行程长度,而不会弄乱你的旋转。

答案 1 :(得分:1)

默认情况下,形状图层中坐标系的原点位于点(0,0),其边界为0×0维;当你给它一个路径时,这些属性都不会自动改变。你给它的弧是以(27,27)为中心,所以当你旋转图层时,绘制的弧围绕图层的实际中心旋转,这是弧的中心上方和左侧27点的点

您最好的选择是更改创建弧形的方式并定位其图层。不是创建中心为(27,27)的Bézier路径,而是给它一个(0,0)-i.e的中心。图层的原点 - 并将弧图层的位置设置为其父图层中的(27,27)。