How to do the flip animation between two UIViewControllers while clicking info button?

53,972

Solution 1

To flip into a view controller:

viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:viewController animated:YES completion:nil];

To flip out of it:

[self dismissViewControllerAnimated:YES completion:nil];

Solution 2

This is how I'm doing it:

 AboutShowViewController *aboutShowViewController = [[AboutShowViewController alloc] initWithNibName:@"AboutShowViewController" bundle:[NSBundle mainBundle]];

    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.80];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight 
                           forView:self.navigationController.view cache:NO];

    [self.navigationController pushViewController:aboutShowViewController animated:YES];
    [UIView commitAnimations];

    [aboutShowViewController release];

Credit goes to faysar here.

Solution 3

There is a fundamental flaw in your understanding of the MVC structure in Cocoa Touch, which is, View Controllers and Views are comparable and similar. The truth is, they are Not.

Back to your specific question, yes, animations are based on views, not on view controllers. But each view should be controlled by one of your view controllers. And this which-view-belongs-to-which-controller thing is totally up to you. In your case, animation could happen between 2 views with 2 different controllers, or, they could also happen between 2 views of the same controller.

As for code samples, I suggest you take a look at one of Xcode's default templates, the Utility Application, which has implemented this click-and-flip animation in a succinct and standardized way.

Solution 4

  override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(true)
      // for back button
      changeTransition()
  }
        //btnMap.addTarget(self, action: #selector(searchHotelsResultVC.goToMap), for: .touchUpInside)
       //btnMap.addTarget(self, action: #selector(MapViewController.backToList), for: .touchUpInside)
func goToMap()  {
      // for pushing
     changeTransition()
navigationController?.pushViewController(settingsVC, animated: false)
}
func backToList()  {
        // for dismiss 
        changeTransition()
        navigationController?.popViewController(animated: false)
        dismiss(animated: true, completion: nil)

    }
    func changeTransition()  {
        let transition = CATransition()
        transition.duration = 0.5
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        //transition.type = kCATransitionPush
        transition.type = "flip"
        transition.subtype = kCATransitionFromLeft
        navigationController?.view.layer.add(transition, forKey: kCATransition)

    }
Share:
53,972
Admin
Author by

Admin

Updated on November 26, 2020

Comments

  • Admin
    Admin over 3 years

    I have a login page named "LoginViewController". I have an info button in this page. If I click on it, I want to show some information about my application. Also I want to present that information page using flip animation.

    The code for creating info button is,

    infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    infoButton.frame = CGRectMake(285, 425, 30, 30);
    infoButton.backgroundColor = [UIColor clearColor];
    
    [infoButton addTarget:self 
                action:@selector(displayInfoView) 
                forControlEvents:UIControlEventTouchUpInside];
    

    If I click on the info button, the displayInfoView method will be called. There I can show a UIView to display some information. Am I right?

    For flip animation, I used this code...

    [UIView beginAnimations:nil context:NULL];
    
    [UIView setAnimationDuration:.8];
    
    [UIView setAnimationTransition:([loginPageView superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) 
            forView:mainView
            cache:YES]; 
    
    if ([infoView superview]) {
        [infoView removeFromSuperview];
        [mainView addSubview:loginPageView];
    }
    else {
        [loginPageView removeFromSuperview];
        [mainView addSubview:infoView];
    }
    
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
    

    Here loginPageView is the login view that contains the info button. infoView is the view that contains the information about application. mainView is the common view that holds the present view.

    Now my problem is, instead of showing a UIView, can I show an another view controller class while clicking the info button? Remember the flip action works fine with UIView. But when I try with a UIViewController, this makes trouble.

    Can someone suggest me how to show an UIViewController ( in my app, I need to show AboutViewController) while clicking the info button with the flip effect?

  • Admin
    Admin over 13 years
    Thank you for your reply... I'm trying to do the animation between two view controllers. Not views
  • Admin
    Admin over 13 years
    It works fine with a viewcontroller and a view. But did not work with two viewcontrollers
  • Di Wu
    Di Wu over 13 years
    Can you edit your code format so I can take a look at it? Now the format is just so messy that I don't even know where to start.
  • Filip Radelic
    Filip Radelic almost 13 years
    Navigation controller has a pretty clear purpose, and so do modal view controllers. Why use one to achieve another's purpose?
  • cheesus
    cheesus over 11 years
    Because Michael's version works without also flipping the UITabBar. Who asked about modal view controllers?
  • Elliot Yap
    Elliot Yap over 10 years
    i thought i need a navigation controller until i read your answer.