2011-03-29 104 views
2

由於某種原因,我必須再次提交相同的動畫。動畫重複

- (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]; 
    } 
} 

但結果是,「稱爲startAnimation:shouldContinue = 1」的文本被一次又一次地迅速打印後cancelAnimation是called.It前一輪好像同時(YES){打印(,,, ); }一個死循環heppens。

回答

0

請指定代碼

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

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

請shouldContinue到伊娃如果不是

如果你讓shouldContinue爲伊娃然後確保指定

self.shouldContinue = NO; 

,你沒叫在任何地方都可以使用取消消除功能,如果不是那麼在開始動畫的結尾處,

self.shouldContinue = NO; 
0

從動畫代碼中刪除此行.. [UIView commitAnimations];它會重複動畫...