我可以使用CGAffineTransformMakeRotation旋转超过360度的视图吗?

我正在写一个iPhone应用程序,我有一个我想要向外旋转的图像。

目前我的代码看起来像这样(包装在一个beginAnimations / commitAnimations块):

scale = CGAffineTransformScale(CGAffineTransformIdentity, 5.0f, 5.0f); swirl = CGAffineTransformRotate(scale, M_PI); [player setTransform:swirl]; [player setAlpha:0.0f]; 

但是我发现如果我试图改变旋转的angular度,比如说4 * M_PI,它根本就不旋转。 是否可以使用CGAffineTransformRotate旋转720˚,还是必须切换到其他技术?

如果我必须切换到另一种技术,你会推荐使用另一个线程(或计时器)来自己做animation,还是OpenGL是一条更好的路线?

谢谢,
布莱克。

无论旋转angular度小于完整旋转angular度还是旋转angular度的许多倍数,都可以将视图旋转一定数量的弧度,而不必将旋转分割为多个部分。 作为一个例子,下面的代码会在一个指定的秒数内旋转一个视图,每秒一次。 您可以轻松修改它以旋转一定数量的旋转视图,或旋转一定数量的弧度。

 - (void) runSpinAnimationWithDuration:(CGFloat) duration; { CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ]; rotationAnimation.duration = duration; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = 1.0; rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } 

你可以,但是你需要把你的animation分成半圈旋转。 我提供了一个例子来回应这个问题 ,使用重复的CABasicAnimation应用于视图下的图层。 正如我在那里所build议的那样,将这些半旋转作为CAKeyframeAnimation的一部分,可能会是更好的方式来构build这个结构,因为animation会更平滑(在我的例子中,在半旋转之间有一个小小的障碍),你可以做在开始和结束一个很好的加速/减速。

标题答案:是的,CGAffineTransform可以旋转360度以上。
回答问题:是的,但是你不能制作animation,因为没有任何animation。

请记住, 仿射变换是一个matrix ,旋转matrix包含预先计算的正弦和余弦数字。 这就好像你说:

 CGFloat angle = 4.0 * M_PI; NSAffineTransformStruct matrix = { .m11 = cos(angle), .m12 = sin(angle), .m21 = -sin(angle), .m22 = cos(angle), .tx = 0.0, .ty = 0.0 }; NSAffineTransform *transform = [NSAffineTransform transform]; [transform setTransformStruct:matrix]; 

现在,让我们回顾一下cossin一些示例值:

  • cos(0π) = 1
  • sin(0π) = 0
  • cos(2π) = 1
  • sin(2π) = 0
  • cos(4π) = 1
  • sin(4π) = 0

请记住, matrix不包含angular度 – 它只包含余弦和正弦 。 而这些价值观并不是从一个圆圈变成另一个圆圈。 因此,没有任何animation。

(请注意,Calculator.app会给cos(xπ)和sin(xπ)带来错误的结果,请尝试Grapher, calc或Google 。)

您需要将animation分成几个小部分。 Brad Larsonbuild议的半圈将会很好。