在开发时,有时候需要实现图片旋转功能,比如说音频播放器实现专辑图片的转动功能。可以用动画来实现:
初始化动画
-(void)initAnimationWithImageView:(UIImageView *)imageView onSpeed:(float)speed
{
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * speed ];
rotationAnimation.duration = 0.2;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = FLT_MAX;
[imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
启动动画
-(void)startAnimationWithImageView:(UIImageView *)imageView
{
CFTimeInterval pausedTime = [imageView.layer timeOffset];
imageView.layer.speed = 1.0;
imageView.layer.timeOffset = 0.0;
imageView.layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [imageView.layer convertTime:CACurrentMediaTime()
fromLayer:nil] - pausedTime;
imageView.layer.beginTime = timeSincePause;
}
暂停动画
-(void)pauseAnimationWithImageView:(UIImageView *)imageView
{
CFTimeInterval pausedTime = [imageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
imageView.layer.speed = 0.0;
imageView.layer.timeOffset = pausedTime;
}
声明:此博文源自 http://blog.csdn.net/shenyuanluo/article/details/49096535
如需转载,请说明博文出处。谢谢!