2014-01-25 59 views
1

我有3個顏色條的3 CABasicAnimation,它們是相互跟隨的。三分之一完成後,3個酒吧將停留在最後的位置。直到這裏,什麼都好,這裏是代碼:ios - cabasicanimation需要重置

- (void)animationDidStop:(CABasicAnimation *)theAnimation finished:(BOOL)flag { 
    NSString* value = [theAnimation valueForKey:@"id"]; 
    if([value isEqualToString:@"position1"]){ 
     [self playVideoAtIndex:1]; 
    } 
    else if([value isEqualToString:@"position2"]){ 
     [self playVideoAtIndex:2]; 
    } 
    else if([value isEqualToString:@"position3"]){ 

    } 
} 

在這之前,我在3個動畫像這樣:

-(void)createAnimationAtIndex:(NSInteger)index{ 
UILabel *label = (UILabel *)[barLabelArray objectAtIndex:index]; 
if(index==0){ 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
    animation.delegate = self; 
    //SOMETHING HERE 
    [label.layer addAnimation:animation forKey:@"position1"]; 
} 
else if(index==1){ 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
    animation.delegate = self; 
    //SOMETHING HERE 
    [self.delegate startPlayVideoAtIndex:1]; 
    [label.layer addAnimation:animation forKey:@"position2"]; 
} 
else if(index==2){ 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
    animation.delegate = self; 
    //SOMETHING HERE 
    [label.layer addAnimation:animation forKey:@"position3"]; 
} 

}

所以,如果我等到所有動畫停止,當我回來時,他們會再次正確地開始動畫。但有時候我需要在中間停止動畫。然後當我回來再次開始動畫時,一切都搞砸了。這裏是停止動畫的代碼:

-(void)reset{ 
    for(UILabel *label in barLabelArray){ 
     [label.layer removeAllAnimations]; 
    } 
} 

那麼你知道我在做什麼錯在這裏,以及如何解決它?謝謝 !!

回答

1

有一種方法可以暫停和恢復任何動畫。這裏有幾個很好的字符串,可以幫助你,如果我理解正確

- (void) pauseLayer 
{ 
    CFTimeInterval pausedTime = [label.layer convertTime:CACurrentMediaTime() fromLayer:nil]; 
    label.layer.speed = 0.0; 
    label.layer.timeOffset = pausedTime; 
} 

- (void) resumeLayer 
{ 
    CFTimeInterval pausedTime = [label.layer timeOffset]; 
    label.layer.speed = 1.0; 
    label.layer.timeOffset = 0.0; 
    label.layer.beginTime = 0.0; 
    CFTimeInterval timeSincePause = [label.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; 
    label.layer.beginTime = timeSincePause; 
}