2013-03-27 48 views
6

我從那個狀態,我CAAnimationGroup兩個旋轉的動畫,一個從零開始,另一種是重複和autoreverses:CAAnimationGroup的最大持續時間值(CFTimeInterval)是多少?

- (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,我嘗試設置動畫組的持續時間到​​,但會阻止動畫組運行。但是,如果我將它設置爲一個很大的數字,例如10000,它運行良好。在CAAnimationGroup的持續時間內,我可以使用的最大數字是多少,以確保其儘可能接近無窮?

UPDATE:看來,如果我把一個非常大的值,例如DBL_MAX/4.0則凍結了一秒鐘,然後開始動畫。如果我輸入值DBL_MAX/20.0,那麼開始時的凍結就會小很多。看起來持續這麼大的價值會導致它凍結。除了在持續時間內使用非常大的值以外,還有更好的方法嗎?

+0

你正在努力使這組動畫的重複下去嗎? – 2013-03-27 17:19:50

+0

是的,但我只希望小組的第二部分能夠永遠重複。 – jowie 2013-03-27 17:22:39

+0

你確定需要設置持續時間嗎?如果讓它未設置會發生什麼? – 2013-03-27 17:25:11

回答

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]; 
} 
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]; 
} 
相關問題