简体   繁体   English

如何绕圆旋转CAShapeLayer弧?

[英]How to rotate a CAShapeLayer arc around a circle?

I have the two CAShapeLayer s forming the following display: 我有两个CAShapeLayer构成以下显示:

在此处输入图片说明

They are created with this piece of code: 它们是使用以下代码创建的:

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

I'd like for the smaller, opaque arc to retain its size and spin around the circle indefinitely. 我希望较小的不透明弧形保持其大小并无限旋转。 I've tried applying a number of transform.rotation animations that I found on other answers , however the arc ends up spinning into unpredictable directions. 我尝试应用在其他 答案上发现的许多transform.rotation动画,但是弧线最终旋转到了不可预测的方向。

I've also tried raising strokeStart and strokeEnd of the arc at the same rate, which gets the effect I want at first but is unable to proceed cyclically due to the nature of those properties. 我还尝试了以相同的速率提高strokeStartstrokeEnd ,这首先获得了我想要的效果,但是由于这些属性的性质而无法循环进行。

That being so, how to perform this effect? 既然如此,该如何发挥作用呢?

By default, the origin of the coordinate system in a shape layer is at the point (0,0), and its bounds have 0×0 dimensions; 默认情况下,形状层中坐标系的原点位于(0,0)点,其边界的尺寸为0×0; when you give it a path, neither of those attributes change automatically. 当您为其指定路径时,这些属性均不会自动更改。 The arc you're giving it is centered at (27, 27), so when you rotate the layer the drawn arc is rotating around the layer's actual center, which is a point 27 points above and to the left of the center of the arc. 您赋予它的弧线的中心为(27,27),因此,当旋转图层时,绘制的弧线将围绕图层的实际中心旋转,该点是圆弧中心上方和左侧的27点。

Your best option is to change the way you're creating the arc shape and positioning its layer. 最好的选择是更改创建弧形和放置其图层的方式。 Instead of creating the Bézier path with a center of (27, 27), give it a center of (0, 0)—ie the origin of the layer—and set the arc layer's position to (27, 27) within its parent layer. 与其创建中心为(27,27)的Bézier路径,不如将其中心设为(0,0)(即图层的原点),并将弧层在其父图层中的位置设置为(27,27) 。

Your best bet here would be to rotate your entire circle. 您最好的选择是旋转整个圈子。 Rotate loadingView around its center and you'll get the effect you want. 围绕其中心旋转loadingView ,您将获得想要的效果。 You'll gain the added benefit of being able to even modify the stroke length very easily without messing up your rotation. 您将获得额外的好处,即可以非常轻松地修改笔画长度而不会弄乱您的旋转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM