效果如下:
把旋转控制器定义为常量,防止被ARC和谐掉
@interface ViewController ()
{
CABasicAnimation *_rotationAnimation;
}
@end
具体方法
-(void)buildUI
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
label.center = self.view.center;
label.text = @"iOS";
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:25];
label.layer.borderWidth = 1.0f;
label.textColor = [UIColor blackColor];
[self.view addSubview:label];
_rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
_rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
_rotationAnimation.duration = 10;
_rotationAnimation.cumulative = YES;
_rotationAnimation.repeatCount = HUGE_VAL;
_rotationAnimation.removedOnCompletion = false;
[label.layer addAnimation:_rotationAnimation forKey:@"rotationAnimation"];
}
参考资料