【问题标题】:CABasicAnimation - view should position on the last frameCABasicAnimation - 视图应位于最后一帧
【发布时间】:2015-08-04 20:30:01
【问题描述】:

您好,我想做一个 CABasicAnimation 旋转,我的视图旋转 440 度。在动画之后,我不想将视图重置为旧位置。它应该与动画最后一帧的位置相同。

CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];

rotate.toValue = [NSNumber numberWithFloat:degrees / 180.0 * M_PI];
rotate.duration = 4.4;
[rotate setFillMode:kCAFillModeForwards];
UIView *animationView = [self getBGContainerViewForSender:sender];
[animationView.layer addAnimation:rotate forKey:@"myRotationAnimation"];</i>

谁能告诉我如何在最后一帧定位视图?

【问题讨论】:

    标签: objective-c iphone ios7 xcode6 cabasicanimation


    【解决方案1】:

    当您创建动画并将其添加到图层时,您不会更改对象中的任何值。所以在你的情况下, animationView 的 transform 属性不会改变。因此,当动画结束时,视图会重新回到原来的位置。要解决这个问题,你必须做两件事:

    1. 在添加动画之前,请将视图层的 transform 属性设置为动画完成后所需的值。但是,这会给图层添加一个隐式动画,所以你需要杀死隐式动画。为此:

    2. 添加动画方法中forKey:的值必须是动画的名称,本例中为“transform”。此名称将用您的显式动画替换转换的隐式动画。

    所以添加:

    animationView.layer.transform = CA3DTransformMakeRotate(....);

    并将添加动画调用更改为

    [animationView.layer addAnimation:rotate forKey:@"transform"];

    这在 WWDC 2010,Core Animation in Practice Part 1 中进行了解释,大约 39:20。

    【讨论】:

      【解决方案2】:

      在启动动画之前进行如下事务:

      [CATransaction begin];
      [CATransaction setValue:kCFBooleanTrue forKey:kCATransactionDisableActions];
      CATransform3D current = animationView.layer.transform;
      animationView.layer.transform = CATransform3DRotate(current, numberWithFloat:degrees / 180.0 * M_PI, 0, 1.0, 0);
      [CATransaction commit];
      
      /* YOUR ORIGINAL CODE COMES HERE*/
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-11
        • 2013-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-08
        • 1970-01-01
        相关资源
        最近更新 更多