iOS 图片360° 平缓旋转

听说项目开发中难免会遇到,让图片旋转起来的(看会旋转的女孩,);于是我就写了一个UIImageView 分类,以供参考学习使用;

分类主要有两个外部方法:

  • (void)rotate360DegreeWithImageView;
  • (void)stopRotate;

鉴于OC语言自注释,我就不解释这两个方法的作用了;

接下来,看内部实现:
让图片旋转的实现:


-(void)rotate360DegreeWithImageView {

CABasicAnimation * rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //让其在z轴旋转
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];//旋转角度
rotationAnimation.duration = 2; //旋转周期
rotationAnimation.cumulative = YES;//旋转累加角度
rotationAnimation.repeatCount = 100000;//旋转次数
[self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

}


让图片停止旋转比较简单:

-(void)stopRotate {

[self.layer removeAllAnimations];

}


最后就是如何调用实现了

首先导入分类头文件

a. 让图片360° 旋转,只需调用rotate360DegreeWithImageView该方法,如下

[imageView rotate360DegreeWithImageView];

b. 停止旋转, 调用stopRotate,如下

[imageView stopRotate];

最后附上分类下载地址

你可能感兴趣的:(iOS 图片360° 平缓旋转)