1

我想讓UIView來回發光兩次。我的動畫在下面的作品中,但在它結束之後,它會轉到我將其設置爲動畫的狀態,而不是原作。我將autoreverses設置爲YES,爲什麼它不回到原始狀態?UIView動畫不會回到原始狀態

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationRepeatCount:2]; 
[UIView setAnimationRepeatAutoreverses:YES]; 
[UIView setAnimationDuration:0.3]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
textDetailView.layer.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.3].CGColor; 
[UIView commitAnimations]; 

回答

1

動畫反轉,但值不。動畫完成後,它將從視圖中刪除(實際上是從視圖圖層中刪除),並且該視圖似乎具有已設置的屬性。

您可以將值設置回完成塊或選擇器中,以便使用舊版UIView動畫。

或者,對於值不變的動畫,您可以使用核心動畫(不會掛起該值(在動畫之後視圖變爲原始狀態))中獲益。

您需要導入QuartzCore.framework幷包含QuartzCore/QuartzCore.h才能使其運行。

的也需要進行更新,以這樣的事:

CABasicAnimation *myColorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"]; 
[myColorAnimation setAutoreverses:YES]; 
[myColorAnimation setRepeatCount:2]; 
[myColorAnimation setDuration:0.3]; 
[myColorAnimation setToValue:(id)[myColor CGColor]]; 
// the default "from value" is the current value 
[[textDetailView layer] addAnimation:myColorAnimation forKey:@"myColorKey"]; 
+0

我使用CABasicAnimation直到我意識到我需要使用「didFinishSelector」做我的動畫完成後的東西。由於我被迫回到這個問題,我如何得到一個信號表明我的CABasicAnimation已經完成了?是否有某種委託方法?謝謝 – pnizzle

+0

@pnizzle在這個特定的情況下,你不需要回調,因爲視圖在動畫之後會有原始的顏色,就像你想要的那樣。這就是爲什麼它適合你的情況。 –

+1

@pnizzle是的,您可以將自己設置爲動畫的'委託',然後您將獲得'animationDidStart:'和'animationDidStop:已完成:' –