ios - cabasicanimation需要重置

时间:2014-01-25 22:43:03

标签: ios animation

我有3个颜色条的3个CABasicAnimation,它们是相互跟随的。第三个完成后,3个小节将保持在最终位置。在此之前,一切都很好,这是代码:

- (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 个答案:

答案 0 :(得分: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;
}