Help for iOS: CATransform3DIdentity

Hi guys, I’m trying to learn Objective C by my self, the books of “Big Nerd Ranch” has been a great help. And by now I’m stuck in some code, and may be some one could help me. Please sorry if I’m wrong posting here for this matter, but I did not found any good help anywhere for my question.

Here is the question:

I want to rotate two images, initially, show just the first image, the second is hidden when the viewDidLoad finish. In the app, the user press the button, the first images begin to rotate, and when is like “completely flat” (like in the screen is 0° and rotate 90°) the code hide it, and show the second image, and begin the rotation of this second image just in the position of the first image ended (beginning in the 90° and ending in 0°), the effect is like turning one images with two faces, and the event is turn it… BUT!!! the first event works fine, but just that!!! The second the animation does not work.

In the main.storyboard the Image01 and the Image02 are the exact size and the same position, one over another. and the button is just below the images

Here is my code:

//  ViewController.h
//  Simple3DAnimation
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *Image01;
@property (strong, nonatomic) IBOutlet UIImageView *Image02;
@property (strong, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)changeImage:(id)sender;

@end


//  ViewController.m
//  Simple3DAnimation
//

#import "ViewController.h"

@interface ViewController ()
{
    BOOL myFlag;  //a flag to determine if show one or another
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //set images into UIImages
    self.Image01.image = [An Image]; //A image of a circle full of red
    self.Image02.image = [An Image]; //A image of a circle full of blue

    //Hide image02
    self.Image02.hidden = YES;

    //just show image 01
    self.Image01.hidden = NO;

    //Set the flag
    myFlag = YES;
}

- (IBAction)changeImage:(id)sender
{
        if (myFlag == YES) //Just to be clear... 
        {
            [UIView animateWithDuration:1.0 animations:^{
                //3D rotation Image01

                CABasicAnimation* animation;
                animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
                animation.fromValue = @(0);
                animation.toValue = @(0.5 * M_PI);
                animation.repeatCount = 1;
                animation.duration = 1.0;

                [self.Image01.layer addAnimation:animation forKey:@"rotation"];

                CATransform3D transform = CATransform3DIdentity;
                transform.m34 = 1.0 / 500.0;
                self.Image01.layer.transform = transform;

            } completion:^(BOOL finished) {
                self.Image01.hidden = YES;
                self.Image02.hidden = NO;
                [UIView animateWithDuration:1.0 animations:^{
                    //3D rotation Image02
                    CABasicAnimation* animation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
                    animation2.fromValue = @(1.5 * M_PI);
                    animation2.toValue = @(2.0 * M_PI);
                    animation2.repeatCount = 1;
                    animation2.duration = 1.0;

                    [self.Image02.layer addAnimation:animation2 forKey:@"rotation"];

                    CATransform3D transform2 = CATransform3DIdentity;
                    transform2.m34 = 1.0 / 500.0;
                    self.Image02.layer.transform = transform2;
                } completion:^(BOOL finished) {
                    ;
                }];

            }];
            myFlag = NO; //here everything work good the very first time only
        }
        else if (myFlag == NO)
        {
            [UIView animateWithDuration:1.0 animations:^{
                //3D rotation Image02
                CABasicAnimation* animation3;
                animation3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
                animation3.fromValue = @(2.0 * M_PI);
                animation3.toValue = @(1.5 * M_PI);
                animation3.repeatCount = 1;
                animation3.duration = 0.50;

                [self.Image02.layer addAnimation:animation3 forKey:@"rotation"];

                CATransform3D transform3 = CATransform3DIdentity;
                transform3.m34 = 1.0 / 500.0;
                self.Image02.layer.transform = transform3;
            } completion:^(BOOL finished) {
                self.Image02.hidden = YES;
                self.Image01.hidden = NO;

                [UIView animateWithDuration:1.0 animations:^{
                    //3D rotation Image01
                    CABasicAnimation* animation4 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];

                    animation4.fromValue = @(0.5 * M_PI);
                    animation4.toValue = @(0.0 * M_PI);
                    animation4.repeatCount = 1;
                    animation4.duration = 1.0;

                    [self.Image01.layer addAnimation:animation4 forKey:@"rotation"];

                    CATransform3D transform4 = CATransform3DIdentity;
                    transform4.m34 = 1.0 / 500.0;
                    self.Image01.layer.transform = transform4;
                } completion:^(BOOL finished) {
                    ;
                }];

            }];
            myFlag = YES;
        }
}
@end

Well… just in case… i found a solution!!!

    if (myFlag == YES)
    {
        myFlag = NO;
        [UIView animateWithDuration:1.0 animations:^{
            self.Image01.image = [Test imageOfCircle002];
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.Image01 cache:YES];
            self.Image01.image = [Test imageOfCircle003];
        }];
    }
    else
    {
        myFlag = YES;
        [UIView animateWithDuration:1.0 animations:^{
            self.Image01.image = [Test imageOfCircle003];
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.Image01 cache:YES];
            self.Image01.image = [Test imageOfCircle002];
        }];
        
    }
}