6
votes

How can I change the image of a UIImageView on half way of a Flip animation. My code does seem to flip the UIImageView and successfully change the image but it is changing in an instant. What I want to achieve is to only change the image once the 180 flip reaches the 90 degree flip. Here is my code:

Create UIImageView on view:

UIImage *image = [UIImage imageNamed:@"fb1.jpg"];
    imageView = [[UIImageView alloc] initWithImage:image];
    vw = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
    vw.layer.cornerRadius = vw.frame.size.width / 2;
    imageView.frame = CGRectMake(20, 20, 80, 80);
    imageView.layer.cornerRadius = imageView.frame.size.width / 2;
    imageView.layer.masksToBounds = YES;
    imageView.layer.borderColor = [[UIColor blackColor] CGColor];
    imageView.layer.borderWidth = 1.0;
    [self.view addSubview: imageView];
    //[self.view addSubview:vw];
    [imageView setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taptap:)];
    [imageView addGestureRecognizer:tap];

Animate UIImageView on Tap:

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                     animations:^(void) {
                         imageView.transform = CGAffineTransformMakeScale(-1, 1);
                         [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionTransitionCrossDissolve
                                          animations:^(void) {
                                              imageView.image = [UIImage imageNamed:@"fb2.jpg"];
                                          } completion:^(BOOL finished) {

                                          }];
                     }
                     completion:^(BOOL finished) {
                     }];

Also, I am doing this animation on a tap gesture on the specific UIImageView.

3
The answer I tried to follow on that link makes my rounded UIImageView square when the flipping starts.SleepNot

3 Answers

22
votes

You can use UIView class method + (void)transitionWithView:duration:options:animations:completion: for this animation easily.

Use code like this for your animation:

[UIView transitionWithView:imageView
                  duration:0.4
                   options:UIViewAnimationOptionTransitionFlipFromRight
                animations:^{
                    //  Set the new image
                    //  Since its done in animation block, the change will be animated
                    imageView.image = newImage;
                } completion:^(BOOL finished) {
                    //  Do whatever when the animation is finished
                }];

You can set direction of flip by replacing UIViewAnimationOptionTransitionFlipFromRight with any of the following options:

UIViewAnimationOptionTransitionFlipFromLeft
UIViewAnimationOptionTransitionFlipFromRight
UIViewAnimationOptionTransitionFlipFromTop
UIViewAnimationOptionTransitionFlipFromBottom
3
votes

For Swift

UIView.transition(with: imageView,
                  duration: 0.4,
                  options: UIView.AnimationOptions.transitionFlipFromLeft,
                  animations: {
                      imageView.image = newImage
        }, completion: nil)
0
votes

Try a two steps animation, if you don't like this you can lookup the documentation for concatenating options.

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn
                 animations:^(void) {
                     //do your half rotation here
                     imageView.transform = CGAffineTransformMakeScale(0, 1);
                 }
                 completion:^(BOOL finished) {
                     if(finished){
                         imageView.image = [UIImage imageNamed:@"fb2.jpg"];
                         [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut
                                          animations:^(void) {
                                              //do your second half rotation here
                                              imageView.transform = CGAffineTransformMakeScale(-1, 1);
                                          } completion:^(BOOL finished) {

                                          }];
                     }
                 }];