2011-05-19 84 views
22

我一直使用Flash,並且在一幀和另一幀之間更改alpha值非常簡單。有沒有辦法在xcode 4中做到這一點?我正在製作徽標,我需要第一個PNG消失,而第二個PNG開始出現。 TNX!動畫alpha更改

+1

你開發iOS或Mac嗎? – Phlibbo 2011-05-19 19:37:02

+0

for iPad上的iOs – 2011-05-19 21:10:30

回答

47

除了esqew的方法(在iOS 4之前可用,所以如果您不打算將工作限制爲僅適用於iOS 4,您應該可以使用它),還有[UIView animateWithDuration:animations:],它允許您執行在一個塊中的動畫。例如:

[UIView animateWithDuration:3.0 animations:^(void) { 
    image1.alpha = 0; 
    image2.alpha = 1; 
}]; 

非常簡單,但同樣只適用於iOS 4,因此請記住這一點。

+0

嗨,我很新,在這個,你怎麼得到兩個圖像在一個uUIView? – 2011-05-20 16:15:27

+0

@ melisa-d我只是假設你的圖像是UIImageView實例,它們是特定視圖的子視圖。那有意義嗎? – nil 2011-05-20 23:14:12

+0

是的,perferct的意義,我刪除了我之前提到過的數組,並單獨的UIViews,但我仍然不能動畫他們 – 2011-05-21 20:10:49

6

實際上這很簡單。將要發生的動畫下面的代碼:

[UIView beginAnimations:NULL context:NULL]; 
[UIView setAnimationDuration:3.0]; // you can set this to whatever you like 
/* put animations to be executed here, for example: */ 
[image1 setAlpha:0]; 
[image2 setAlpha:1]; 
/* end animations to be executed */ 
[UIView commitAnimations]; // execute the animations listed above 

你可以閱讀更多關於this document這些方法。

如果你想與你在這個問題上您的評論提到了一個結構工作:

[UIView beginAnimations:NULL context:NULL]; 
[UIView setAnimationDuration:3.0]; // you can set this to whatever you like 
/* put animations to be executed here, for example: */ 
[[introAnimation objectAtIndex:0] setAlpha:0]; 
[[introAnimation objectAtIndex:1] setAlpha:1]; 
/* end animations to be executed */ 
[UIView commitAnimations]; // execute the animations listed above 

...應該工作。

+0

如果我正在處理這個問題:NSArray * introAnimation; \t introAnimation = [[NSArray中的alloc] initWithObjects: \t \t \t [UIImage的imageNamed:@ 「前奏000.jpg」], \t \t \t [UIImage的imageNamed:@ 「前奏001.JPG」],零]。 – 2011-05-20 16:12:24

+0

我更新了我的答案,以納入你的'NSArray'。 – esqew 2011-05-20 20:09:32

11

其他的解決方案,淡入和淡出:

//Disappear 
[UIView animateWithDuration:1.0 animations:^(void) { 
     SplashImage.alpha = 1; 
     SplashImage.alpha = 0; 
} 
completion:^(BOOL finished){ 
//Appear 
    [UIView animateWithDuration:1.0 animations:^(void) { 
     [SplashImage setImage:[UIImage imageNamed:sImageName]]; 
     SplashImage.alpha = 0; 
     SplashImage.alpha = 1; 
}]; 
}];