Question

The code below fades out the current image, but it results in all buttons disappearing for some odd reason.

self.imageThree = [UIImage imageNamed:@"newscreensh.png"];
self.imageTwo = [UIImage imageNamed:@""];

UIImage * toImage = ([self.imageView.image isEqual:self.imageThree]) ? self.imageTwo : self.imageThree;

[UIView transitionWithView:self.imageView
                  duration:0.8f
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                      self.imageView.image = toImage;
               }completion:NULL];

When I use this code below, all buttons do not disappear.

loader.image = [UIImage imageNamed:@""];

However, I need a fade effect to smooth things out. What could possibly be happening to my storyboard buttons?

Was it helpful?

Solution

self.imageThree = [UIImage imageNamed:@"newscreensh.png"];
//self.imageTwo = [UIImage imageNamed:@""];   

UIImage *toImage = self.imageView.image != nil?nil:self.imageThree;

Since isEqual will compare whether two image's data have the same number of bytes,and [UIImage imageNamed:@""] return nil value, so you only need to compare wether imageView's image is nil.return nil or imageThree to achieve fade out animation.

[UIView transitionWithView:self.imageView
                  duration:0.8f
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                      self.imageView.image = toImage;
               }completion:NULL];

loader.image = nil; //you don't have to use [UIImage imageNamed:@""] to remove imageView's image.

OTHER TIPS

To fade an image, you can change its alpha value like this:

[self.imageTwo setAlpha:0.5];

Here if alphaValue is 0, image would be completely transparent. Alpha values ranges between 0 & 1. Let me know if this is not what you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top