CAAnimationGroup的最大持续时间值(CFTimeInterval)是多少?

时间:2013-03-27 17:06:37

标签: ios objective-c ipad core-animation caanimation

我的CAAnimationGroup中有两个旋转动画,一个从零开始,另一个重复并从该状态自动反转:

- (void)addWobbleAnimationToView:(UIView *)view amount:(float)amount speed:(float)speed
{
    NSMutableArray *anims = [NSMutableArray array];

    // initial wobble
    CABasicAnimation *startWobble = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    startWobble.toValue = [NSNumber numberWithFloat:-amount];
    startWobble.duration = speed/2.0;
    startWobble.beginTime = 0;
    startWobble.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [anims addObject:startWobble];

    // rest of wobble
    CABasicAnimation *wobbleAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    wobbleAnim.fromValue = [NSNumber numberWithFloat:-amount];
    wobbleAnim.toValue = [NSNumber numberWithFloat:amount];
    wobbleAnim.duration = speed;
    wobbleAnim.beginTime = speed/2.0;
    wobbleAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    wobbleAnim.autoreverses = YES;
    wobbleAnim.repeatCount = INFINITY;
    [anims addObject:wobbleAnim];

    CAAnimationGroup *wobbleGroup = [CAAnimationGroup animation];
    wobbleGroup.duration = DBL_MAX; // this stops it from working
    wobbleGroup.animations = anims;

    [view.layer addAnimation:wobbleGroup forKey:@"wobble"];
}

由于CFTimeInterval被定义为double,我尝试将动画组的持续时间设置为DBL_MAX,但这会阻止动画组运行。但是,如果我将它设置为一个大数字,如10000,它运行正常。我可以在CAAnimationGroup的持续时间内使用的最大数字是多少,以确保它尽可能接近无穷大?

更新:如果我放入一个非常大的值,例如DBL_MAX / 4.0,那么它会冻结一秒,然后开始制作动画。如果我输入值DBL_MAX / 20.0,则开头的冻结要小得多。似乎在持续时间内具有如此大的值会导致它冻结。除了在持续时间内使用非常大的值之外,还有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

我现在面对完全相同的问题...我希望有人证明我错了,但我认为处理这种情况的唯一合理方法是将第一个动画移动到CATransaction,然后链接使用自动反转动画:

[CATransaction setCompletionBlock:block];

这不理想,但完成工作。

关于从后台回来时暂停动画的问题,这是CoreAnimation框架的一个典型限制,已经为它提出了许多解决方案。我解决它的方法是通过随机化timeOffset属性,简单地在动画的随机点重置动画。由于应用程序在后台,用户无法确切地知道动画状态应该是什么。以下是一些可以提供帮助的代码(查找//RANDOMIZE!!评论):

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(startAnimating)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:[UIApplication sharedApplication]];

...

for (CALayer* layer in _layers) 
{
    // RANDOMIZE!!!
    int index = arc4random()%[levels count];
    int level = ...;
    CGFloat xOffset = ...;

    layer.position = CGPointMake(xOffset, self.bounds.size.height/5.0f + yOffset * level);

    CGFloat speed = (1.5f + (arc4random() % 40)/10.f);
    CGFloat duration = (int)((self.bounds.size.width - xOffset)/speed);

    NSString* keyPath = @"position.x";
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:keyPath];

    anim.fromValue      = @(xOffset);
    anim.toValue        = @(self.bounds.size.width);
    anim.duration       = duration;
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    // RANDOMIZE!!
    anim.timeOffset     = arc4random() % (int) duration;
    anim.repeatCount    = INFINITY;

    [layer removeAnimationForKey:keyPath];
    [layer addAnimation:anim forKey:keyPath];
    [_animatingLayers addObject:layer];
}

答案 1 :(得分:0)

使用单个关键帧动画而不是一组两个单独的动画要简单得多。

- (void)addWobbleAnimationToView:(UIView *)view amount:(CGFloat)amount speed:(NSTimeInterval)speed {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 2 * speed;
    animation.values = @[ @0.0f, @(-amount), @0.0f, @(amount), @0.0f ];
    animation.keyTimes = @[ @0.0, @0.25, @0.5, @0.75, @1.0 ];
    CAMediaTimingFunction *easeOut =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    CAMediaTimingFunction *easeIn =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    animation.timingFunctions = @[ easeOut, easeIn, easeOut, easeIn ];
    animation.repeatCount = HUGE_VALF;
    [view.layer addAnimation:animation forKey:animation.keyPath];
}