【问题标题】:rotate a UIView around its center but several times围绕其中心旋转 UIView 但几次
【发布时间】:2010-10-05 20:01:51
【问题描述】:

我正在尝试围绕其中心旋转一些 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

【问题讨论】:

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


【解决方案1】:

您可以在 UIView 的 layer 属性上使用以下动画。我已经测试过了。

Objective-C

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

Swift 5.0

let viewToSpin = UIView() // However you have initialized your view
let spinAnimation = CABasicAnimation.init(keyPath: "transform.rotation")
spinAnimation.toValue = NSNumber(value: 5.0 * 2.0 * Float.pi)
viewToSpin.layer.add(spinAnimation, forKey: "spinAnimation")

【讨论】:

  • 我怎样才能减慢这个动画的速度?
  • 有关动画时序的考虑,请参阅 CABasicAnimation 实现的 CAMediaTiming 协议的参考资料。特别是,您可能想要设置“持续时间”属性。上面的代码使用的是 0.25 秒的默认持续时间。
【解决方案2】:

正如 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 属性控制加速和减速(以及中间步数的计算)。

【讨论】:

    【解决方案3】:

    获得连续旋转效果有点棘手,但我描述了一种方法来做到这一点here。是的,Core Animation 似乎优化了单位圆内最近结束位置的变换。我在那里描述的方法将几个半旋转动画链接在一起以进行完整旋转,尽管您确实注意到从一个动画到下一个动画的切换中有轻微的卡顿。

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

    【讨论】:

      【解决方案4】:
      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"];
      

      【讨论】:

        猜你喜欢
        • 2014-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-03
        相关资源
        最近更新 更多