动画重复

时间:2011-03-29 08:32:27

标签: iphone objective-c animation uiview

由于某种原因,我必须再次提交相同的动画。

- (void)startAnimation {
    NSLog(@"startAnimation called:shouldContinue = %u",shouldContinue);
    shouldContinue = YES;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:3];
    [UIView setAnimationRepeatCount:10];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop: finished: context:)];
    // bulabula...
    [UIView commitAnimations];
}

- (void)cancelAnimation {
    shouldContinue = NO;
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished    context:(void *)context {
    NSLog(@"animationDidStop called:shouldContinue = %u",shouldContinue);
    if (shouldContinue) {
        [self startAnimation];
    }
}

但结果是在调用cancelAnimation之前一次又一次地快速打印文本“startAnimation called:shouldContinue = 1”。看起来好像是while(YES){print(,,,);一个死循环heppens。

2 个答案:

答案 0 :(得分:0)

请指明该代码

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished    context:(void *)context
{
    NSLog(@"animationDidStop called:shouldContinue = %u",shouldContinue);

    if (shouldContinue!=NO) {
        [self startAnimation];
    }
}

如果不是

请尽快继续ivar

如果你将shouldContinue作为ivar,那么请确保指定

self.shouldContinue = NO;

并且您没有在任何地方调用取消激活函数,如果没有,那么在开始动画的特定结束时

self.shouldContinue = NO;

答案 1 :(得分:0)

从动画代码中删除此行。 [UIView commitAnimations]; 它会重复动画......