顺时针旋转iOS图像



我正在尝试通过顺时针旋转和收缩来动画我的图像。到目前为止,它只是逆时针的。我已经尝试了沿Z旋转的值/键路径的正负值,但它没有改变任何东西。

[window addSubview:splashView];
    [window bringSubviewToFront:splashView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    splashView.frame = CGRectMake(160, 284, 0, 0);
    [splashView.layer setValue:[NSNumber numberWithInt:-360]
                       forKeyPath:@"transform.rotation.z"];
    [UIView commitAnimations];

要使在特定方向旋转超过360°,请使用CAKeyframeAnimation。在这里,您不仅可以设置最终值,还可以设置一些中间值

// consider this to be pseudo code
keyFrameAnimation.values = @[ 90, 180, 270, 360, 450, 540, ... ];

我没有任何代码给你看,但这绝对是这样做的

或者直接使用M_PI

  • M_PI_4 = 45°
  • M_PI_2 = 90°
  • M_PI = 180°
  • M_PI*2 = 360°

你也可以更容易地设置你的转换。

// set transform
splashView.layer.transform = CATransform3DMakeRotation(-M_PI*2, 0, 0, 1);
不是

[splashView.layer setValue:[NSNumber numberWithInt:-360] 
                forKeyPath:@"transform.rotation.z"];

这是因为当动画想要弧度时,你给它传递了度。下面是一个如何进行转换的示例。

float degrees = 90.0f;
float radians = degrees * (180.0f / M_PI);

最新更新