iPhone:UIImageView淡入 – 如何?

我有一个UIImageView ,我想淡入。

有没有办法让一个底层的UIViewController

我已经翻译了最简单的答案,虽然他们都工作,为MonoTouch用户的C# .NET

 public override void ViewDidAppear (bool animated) { base.ViewDidAppear (animated); UIView.BeginAnimations ("fade in"); UIView.SetAnimationDuration (1); imageView.Alpha = 1; UIView.CommitAnimations (); } 

最初将imageview的alpha设置为0,如imageView.alpha = 0;

 - (void)fadeInImage { [UIView beginAnimations:@"fade in" context:nil]; [UIView setAnimationDuration:1.0]; imageView.alpha = 1.0; [UIView commitAnimations]; } 

将animation持续时间更改为您想要的长度。

 UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]]; myImageView.center = CGPointMake(100, 100); myImageView.alpha = 0.0; [self.view addSubview:myImageView]; [UIView animateWithDuration:5.0 animations:^{ myImageView.alpha = 1.0; }]; 

这个简单的代码:

 [UIView animateWithDuration:5.0 animations:^{ theImage.alpha = 1.0; }]; 

UIImage在视图中,并通过IBOutlet连接,也可以从实用程序面板设置Alpha。

在你的UIViewController下面使用

 // add the image view [self.view addSubview:myImageView]; // set up a transition animation CATransition *animate = [CATransition animation]; [animate setDuration:self.animationDelay]; [animate setType:kCATransitionPush]; [animate setSubtype:kCATransitionFade]; [animate setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; [[self layer] addAnimation:animate forKey:@"fade in"]; 

我会使用UIView的+ transitionWithView:duration:options:animation:完成:它是非常有效和强大的。

 [UIView transitionWithView:imgView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ imgView.image = [UIImage imageNamed:@"MyImage"]; } completion:nil]; 

你可以使用这个代码:

淡入animation:

 self.yourComponent.alpha = 0.0f; [UIView beginAnimations:@"fadeIn" context:nil]; [UIView setAnimationDuration:1.0]; // Time in seconds self.yourComponent.alpha = 1.0f; 

淡出animation:

 self.yourComponent.alpha = 1.0f; [UIView beginAnimations:@"fadeOut" context:nil]; [UIView setAnimationDuration:1.0]; // Time in seconds self.yourComponent.alpha = 0.0f; 

self.yourComponent可以是UIViewUIImageViewUIButton或任何其他组件。

Swift版本

 func fadeIn(){ UIView.beginAnimations("fade in", context: nil); UIView.setAnimationDuration(1.0); imageView.alpha = 1.0; UIView.commitAnimations(); }