How to do the flip animation between two UIViewCon

2019-01-30 00:04发布

问题:

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?

回答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];


回答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.



回答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.



回答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)

    }


回答5:

simple lines of code

YourViewController *city = [self.storyboard instantiateViewControllerWithIdentifier:@"YourViewController"];
[UIView beginAnimations:nil context:nil];
[UIView  setAnimationDuration:.5];
[self.navigationController pushViewController:city animated:YES];
[UIView  setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView commitAnimations];