使用CABasicAnimation不止一次旋转UIImageView [英] Using CABasicAnimation to rotate a UIImageView more than once

查看:106
本文介绍了使用CABasicAnimation不止一次旋转UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CABasicAnimation顺时针旋转 UIImageView 90度,但我需要让它在最初90度后从其位置再旋转90度旋转。

I am using a CABasicAnimation to rotate a UIImageView 90 degrees clockwise, but I need to have it rotate a further 90 degrees later on from its position after the initial 90 degree rotation.

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.duration = 10;
animation.additive = YES;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.fromValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(0)];
animation.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(90)];
[_myview.layer addAnimation:animation forKey:@"90rotation"];

使用上面的代码最初工作时,图像保持90度角。如果我再次调用它使其再旋转90度,则动画开始跳回0并旋转90度,而不是90度到180度。

Using the code above works initially, the image stays at a 90 degree angle. If I call this again to make it rotate a further 90 degrees the animation starts by jumping back to 0 and rotating 90 degrees, not 90 to 180 degrees.

我在 animation.additive = YES; 的印象会导致进一步的动画使用当前状态作为起点。

I was under the impression that animation.additive = YES; would cause further animations to use the current state as a starting point.

任何想法?

推荐答案

传递角度的增量值请参阅我的代码

pass incremental value of angle see my code

static int imgAngle=0;
- (void)doAnimation
{
  CABasicAnimation *animation = [CABasicAnimation   animationWithKeyPath:@"transform.rotation.z"];
   animation.duration = 5;
  animation.additive = YES;
  animation.removedOnCompletion = NO;
  animation.fillMode = kCAFillModeForwards;
  animation.fromValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(imgAngle)];
  animation.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(imgAngle+90)];
 [self.imgView.layer addAnimation:animation forKey:@"90rotation"];

  imgAngle+=90;
  if (imgAngle>360) {
      imgAngle = 0;
  }
}

以上代码仅供参考。它没有经过测试

Above code is just for idea. Its not tested

这篇关于使用CABasicAnimation不止一次旋转UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆