【问题标题】:CAAnimation with callback on step带有回调的 CAAnimation
【发布时间】:2013-12-27 17:22:18
【问题描述】:

我有一个使用计时功能的CAAnimation。我需要回调才能始终如一地考虑动画。类似于jQuery's step 回调。我已经在互联网上搜索了解决此问题的方法,但一直找不到。 (也许我没有正确搜索)

到目前为止我的代码:

CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:rotation / 180.0 * M_PI];
rotationAnimation.duration = duration;
rotationAnimation.repeatCount = 0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

[_image.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

我知道委托有这两种方法:

– animationDidStart:
– animationDidStop:finished:

如果有一种方法可以创建一个类别来实现一个,那就太好了

- animationProgress:

或类似的东西。或者CAAnimations 不是解决方案。

如何使用CAAnimations 或任何替代方法来实现这一目标?

【问题讨论】:

    标签: objective-c animation callback caanimation


    【解决方案1】:
        CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:<AnimatableProperty>];
        CALayer *layerToAnimate=[CALayer layer];
    
        [CATransaction begin];
        [CATransaction setCompletionBlock:^{
           //Stuff to be done on completion
        }];
        [layerToAnimate addAnimation:basicAnimation forKey:@"myCustomAnimation"];
        [CATransaction commit];
    

    【讨论】:

      【解决方案2】:

      你可能想使用

      UIView 的 animateWithDuration:delay:options:animations:completion:

      一个简单的设置方法是

      - (void)animateStuff {
       [UIView animateWithDuration:duration
                             delay:delay
                           options:UIViewAnimationOptionTransitionNone
                        animations:^{ // custom animations }
                        completion:^(BOOL finished) {
                             // check if you want to continue with
                             if (<check to continue>) {
                                 [self animateStuff];
                             }
                        }];
      }
      

      如果您可以将整个动画分解成小片段,这将非常有用。使用上面的代码,您可以为每个较小的片段设置动画并继续这样做,直到整个动画完成。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-20
        • 1970-01-01
        相关资源
        最近更新 更多