我有一个使用CAShapeLayer绘制的弧线,我希望它围绕圆圈的中心旋转 . 我试图在'transform.rotation'属性上使用CABasicAnimation来获取此动画 . 但旋转似乎发生在一个与圆圈中心不同的点上 .
-(void)drawRect:(CGRect)rect{
int radius = 100;
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius].CGPath;
circle.fillColor = [UIColor orangeColor].CGColor;
circle.strokeColor = [UIColor blueColor].CGColor;
circle.lineWidth = 5;
circle.strokeStart = 0.0f;
circle.strokeEnd = 0.1f;
[self.layer addSublayer:circle];
CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spinAnimation.byValue = [NSNumber numberWithFloat:2.0f*M_PI];
spinAnimation.duration = 4;
spinAnimation.repeatCount = INFINITY;
[circle addAnimation:spinAnimation forKey:@"indeterminateAnimation"];
}
我知道这是一个老问题,但最终会有什么问题 . 通过设置图层的边界或锚点,我已经修改了很多,但它还没有围绕中心旋转 .
circle.bounds = self.frame;
以下是我将视图添加到视图控制器的方法 .
[self.view addSubview:[[ProgressIndicator alloc] initWithFrame:CGRectMake(50, 50, 200, 200)]];
2 回答
这就是我最终做的 . 错误是将动画添加到子图层而不是图层 . 当只需要围绕圆心时需要旋转时,无需修改anchorPoint或位置 .
想做这样的事情会更好)
和动画:
Swift 3