2012-07-03 19 views
3

我知道這個問題被問到很多,但我已經嘗試了所有其他答案,我無法弄清楚爲什麼動畫停止選擇器沒有被調用。下面是代碼:setAnimationDidStopSelector:沒有被調用

-(void) moveImage:(UIImageView *)image duration:(NSTimeInterval)duration curve:(int)curve x:(CGFloat)x y:(CGFloat)y annKey:(NSString *) annKey{ 

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:duration]; 
[UIView setAnimationCurve:curve]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)]; 
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y); 
image.transform = transform; 
[UIView beginAnimations:annKey context:NULL]; 
[UIView commitAnimations]; 

}

這是一個被髮送的所有正確的參數主要動畫功能。

- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context 
{ 
if ([animationID isEqualToString:@"Burn"]) 
{ 
    NSLog(@"Animation: %@ has finished.",animationID); 
} 

NSLog(@"This does not get called, why not?"); 
} 

我的NSLogs都沒有顯示文本。我究竟做錯了什麼?

+0

爲什麼你有兩個'beginAnimations'? – RyJ

+0

對不起,這是一個錯誤,我已經從代碼中刪除了第一個。但問題依然存在。 – JH95

+0

移動此行,'[UIView beginAnimations:annKey context:NULL];'移動到'moveImage:duration:curve:x:y:annKey:'方法的頂部。 – titaniumdecoy

回答

8

你需要做壽 -

[UIView beginAnimations:annKey context:NULL]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)]; 

// Now define rest. 
[UIView setAnimationDuration:duration]; 
[UIView setAnimationCurve:curve]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y); 
image.transform = transform; 

[UIView commitAnimations]; 
+0

謝謝!現在工作! – JH95

3

不知道是什麼問題,但你真的不應該使用beginAnimations/commitAnimations來。相反,使用塊動畫。您直接在通話的完成部分定義完成代碼。

[UIView animateWithDuration:2.0 
         delay:0.0 
        options:UIViewAnimationOptionCurveEaseOut 
       animations:^{ 
        // animation code 
       } 
       completion:^(BOOL finished){ 
        // completion code 
       }]; 
相關問題