iOS 图片360° 平缓旋转

作者: Ashen | 来源:发表于2015-12-17 10:47 被阅读2770次

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

<b>分类主要有两个外部方法:</b>

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

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

<b>接下来,看内部实现:</b>
让图片旋转的实现:
<pre><code>
-(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"];

}
</code></pre>
让图片停止旋转比较简单:
<pre><code>
-(void)stopRotate {

[self.layer removeAllAnimations];

}
</code></pre>
<b>最后就是如何调用实现了</b>

首先导入分类头文件 </br>
a. 让图片360° 旋转,只需调用rotate360DegreeWithImageView该方法,如下</br>

[imageView rotate360DegreeWithImageView];

b. 停止旋转, 调用stopRotate,如下</br>

[imageView stopRotate];

最后附上分类下载地址

相关文章

网友评论

  • 蜿蜒花骨朵:你这样瞬间回到原位了。停的时候怎么保持到旋转的位置
  • 童话镇里蜿蜒的河:楼主你好 在模态出现的控制器中 动画会失效。 这是为啥
  • hhgvg:你这个是怎么调用的 没看到方法里面有图片对象赋值啊
    Ronda:@hhgvg 这是UIImageVeiw的分类,self就是代表UIImageView的对象
  • LoveLinXue:@曾樑 我想说的也是。太佩服你了
  • 曾樑::+1::+1:
    我的天空蔚蓝色:@阿达西 真是,我发现哪都有他!
    eaf6d184a220:@曾樑 怎么哪都有你🙃🙃🙃

本文标题:iOS 图片360° 平缓旋转

本文链接:https://www.haomeiwen.com/subject/sinuhttx.html