围绕其中心旋转UIView几次

时间:2009-02-05 23:49:14

标签: ios objective-c cocoa-touch uiview cgaffinetransform

我正在尝试围绕其中心旋转一些UIView,因此简单的代码类似于 (伪代码):

[UIView beginAnimations:@"crazyRotate" context:nil];
[UIView setAnimationDuration:1.0];
someview.transform = CGAffineTransformMakeRotation(angle);
[UIView commitAnimations]

现在,如果我设置角度来说M_PI / 2,那么事情就会很好地旋转。 如果我把它设置为2 * M_PI,它确实“没有”。我可以理解矩阵转化为无效的东西(旋转360意味着“停留”在某种意义上), 然而,我想把它旋转5次(想想一个报纸的旋转比例对你有影响 - 我不是很擅长描述,希望有人理解)。 所以,我尝试将设置角度添加到180度(M_PI)并添加嵌套animatationBlock。 但我想,因为我再次设置相同的属性(someview.transition),它会以某种方式忽略它。 我尝试将动画的重复计数设置为2,角度为M_PI,但它似乎只是旋转180,回到直线位置然后再次启动旋转。

所以,我有点想法, 任何帮助赞赏! --t

4 个答案:

答案 0 :(得分:36)

您可以在UIView的图层属性上使用以下动画。我测试了它。

UIView *viewToSpin = ...;    
CABasicAnimation* spinAnimation = [CABasicAnimation
                                  animationWithKeyPath:@"transform.rotation"];
spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI];
[viewToSpin.layer addAnimation:spinAnimation forKey:@"spinAnimation"];

答案 1 :(得分:6)

正如Brad Larson所说,你可以用CAKeyframeAnimation来做到这一点。例如,

CAKeyframeAnimation *rotationAnimation;
rotationAnimation = 
   [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

rotationAnimation.values = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0 * M_PI], 
                            [NSNumber numberWithFloat:0.75 * M_PI], 
                            [NSNumber numberWithFloat:1.5 * M_PI], 
                            [NSNumber numberWithFloat:2.0 * M_PI], nil]; 
rotationAnimation.calculationMode = kCAAnimationPaced;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.timingFunction = 
   [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotationAnimation.duration = 10.0;

CALayer *layer = [viewToSpin layer];
[layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

您可以使用rotationAnimation.duration属性控制整个动画的持续时间,并使用rotationAnimation.timingFunction属性控制加速和减速(以及两者之间的步骤计算)。

答案 2 :(得分:1)

获得连续旋转效果有点棘手,但我描述了一种方法here。是的,Core Animation似乎优化了转换到单位圆内最接近的结束位置。我描述的方法将一些半旋转动画链接在一起以进行完全旋转,尽管你注意到从一个动画到下一个动画的切换中有轻微的断言。

使用这些半旋转值构造的CAKeyframeAnimation也许是正确的方法。然后你也可以控制加速和减速。

答案 3 :(得分:1)

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 8.0f;
animation.repeatCount = INFINITY;
[self.myView.layer addAnimation:animation forKey:@"SpinAnimation"];
相关问题