2011-05-19 83 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; 
}]; 
}];