19

I have an UIImageView that I want to fade in.

Is there a way to enable that on an underlying UIViewController?

I have translated the simplest answer, though they all work, C# .NET for the MonoTouch users:

public override void ViewDidAppear (bool animated)
{
    base.ViewDidAppear (animated);
    UIView.BeginAnimations ("fade in");
    UIView.SetAnimationDuration (1);
    imageView.Alpha = 1;
    UIView.CommitAnimations ();
}
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
Ian Vink
  • 66,960
  • 104
  • 341
  • 555

7 Answers7

30

Initially set the alpha of your imageview as 0 as imageView.alpha = 0;

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

}
visakh7
  • 26,380
  • 8
  • 55
  • 69
23

Change the animation duration to what ever length you want.

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;
}];
Sabobin
  • 4,256
  • 4
  • 27
  • 33
  • IS it possible to only animate the UIImageView and not the UIView? (and if so how?) – clankill3r May 12 '13 at 10:59
  • worked for me: I've added a `UIImageView` from the UI editor and after _(i needed to fade out the imageview)_ simply added the last 3 rows of the example _(with alpha = 0.0)_ and is ok! – ghiboz May 13 '14 at 12:40
4

this simple code:

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

the UIImage is in the view and is connected via an IBOutlet you can also, set the alpha from the utility panel.

Milad
  • 1,239
  • 3
  • 19
  • 37
4

Use below in your 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"];
Nagarjun
  • 6,557
  • 5
  • 33
  • 51
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
1

I would use UIView's +transitionWithView:duration:options:animations:completion: It is very efficient and powerful.

[UIView transitionWithView:imgView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    imgView.image = [UIImage imageNamed:@"MyImage"];
} completion:nil];
Nagarjun
  • 6,557
  • 5
  • 33
  • 51
Tibidabo
  • 21,461
  • 5
  • 90
  • 86
0

You can use this code:

Fade In Animation:

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

Fade Out 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 can be a UIView, UIImageView, UIButton or any other component.

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
0

Swift version

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