-1

i'm trying to animate a UIImageView to change UIImage during the animation, but the duration in the method doesn't work, the animation is done immediately, why? this is the code:

- (IBAction)do_action:(id)sender {
     [UIView animateWithDuration:3.0 animations:^{
         [self.my_image setImage:[UIImage imageNamed:@"image1"]];
     } completion:^(BOOL finished) {
         [self.my_image setImage:[UIImage imageNamed:@"image2"]];
     }];
}

i have inserted 3 second for the animation duration, but doesn't work, do it immediately , how i can do?

Piero
  • 9,173
  • 18
  • 90
  • 160
  • Check this http://stackoverflow.com/questions/7638831/fade-dissolve-when-changing-uiimageviews-image/38350024#38350024 – Kumar KL Jul 13 '16 at 11:18

2 Answers2

0

SetImage is not an animatable Behavior. Easier way to accomplish is to use setAnimationImages property of UIImageView

//set Your Image names as array
[self.imageView setAnimationImages:[NSArray arrayWithObjects:@"IMG_1_Name",@"IMG_2_Name",nil]];

[self.imageView setAnimationRepeatCount:1];
[self.imageView setAnimationDuration:0.5]; //Your total cycle time. here 0.25 sec for each image
[self.imageView startAnimating];
nprd
  • 1,942
  • 1
  • 13
  • 16
0

Try this:

self.my_image.image = [UIImage imageNamed:@"image1"];

CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;

[self.my_image.layer addAnimation:transition forKey:nil];

To set the second image, you need to implement a delegate to get notified about the event when the animation completes.

CATransition has a delegate method animationDidStop:finished: which you can use. Set the delegate property and implement the method:

transition.delegate = self;

...

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
     [self.my_image setImage:[UIImage imageNamed:@"image2"]];
}
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • thanks for the answer, but where i have to insert the image2? – Piero May 18 '14 at 22:26
  • if i use the delegate i doesn't see the transition between the two image, instead if i set the image2 before CATransition *transition = [CATransition animation]; without the delegate i see the transition... – Piero May 18 '14 at 22:34