我用CAShapeLayer画了一个弧线,现在想让它绕圆心旋转。我尝试使用CABasicAnimation的'transform.rotation'属性来实现这个动画。但是旋转似乎发生在不同于圆心的点上。
-(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)]];