开发项目过程中,经常会做一些动画,比如进入某些页面需要让一个view进行360°旋转等等。结合网友们的经验,觉得下面的这些代码用起来还是比较方便的,就总结一下,方便下次使用。
创建一个UIImageView,并添加图片。
view1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 380, 380)];
view1.image=[UIImage imageNamed:@"八卦"];
view1.center=self.view.center;
view1.alpha=1;
[self.view addSubview:view1];
添加动画:
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 20;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 99999;//重复次数
[view1.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
旋转效果如下图: