Search code examples
iosuiviewuiviewanimationuiviewanimationtransition

Animations/transitions when switching between views iOS8


I'm very new to iOS programming and I have two simple UIView's.

.h file:

@property (strong, nonatomic) IBOutlet UIView *mainPageOutlet;
@property (strong, nonatomic) IBOutlet UIView *secondPageOutlet;

.m file:

- (IBAction)secondPageToMain:(id)sender {
    self.view = [self mainPageOutlet];
}

- (IBAction)mainPageToSecondPage:(id)sender {
    self.view = [self secondPageOutlet];
}

How can I use animations or transitions when switching between these two views? Currently they only appear when the button is clicked. Ideally I'd like to have something like push, modal, slide in or up.

Is there a way of doing this?

Many thanks.


Solution

  • Use following code.

    - (IBAction)secondPageToMain:(id)sender {
    
       [UIView transitionFromView:[self secondPageOutlet]
                            toView:[self mainPageOutlet]
                          duration:2
                           options: UIViewAnimationOptionTransitionFlipFromRight
                        completion:^(BOOL finished){
                            [[self secondPageOutlet] sendSubviewToBack];
                        }];
    } 
    
    - (IBAction)mainPageToSecondPage:(id)sender {
    
        [UIView transitionFromView:[self mainPageOutlet]
                            toView:[self secondPageOutlet]
                          duration:2
                           options: UIViewAnimationOptionTransitionFlipFromRight
                        completion:^(BOOL finished){
                            [[self mainPageOutlet] sendSubviewToBack];
                        }];
    }
    

    And in your viewDidLoad method add following code,

        [[self secondPageOutlet] sendSubviewToBack];
    

    So initially your second view will be behind your main view and on performing action it will come in front with animation.