ios 单击信息按钮时如何在两个 UIViewController 之间进行翻转动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4622996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:27:30  来源:igfitidea点击:

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

iosuiviewuiviewcontrollerflip

提问by Filip Radelic

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.

我有一个名为“LoginViewController”的登录页面。我在此页面中有一个信息按钮。如果我点击它,我想显示一些关于我的应用程序的信息。此外,我想使用翻转动画呈现该信息页面。

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 displayInfoViewmethod will be called. There I can show a UIViewto display some information. Am I right?

如果我单击信息按钮,该displayInfoView方法将被调用。在那里我可以显示一个UIView来显示一些信息。我对吗?

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 loginPageViewis the login view that contains the info button. infoViewis the view that contains the information about application. mainViewis the common view that holds the present view.

loginPageView是包含信息按钮的登录视图。infoView是包含有关应用程序信息的视图。mainView是持有当前观点的普遍观点。

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.

现在我的问题是,我UIView可以在单击信息按钮时显示另一个视图控制器类,而不是显示?请记住翻转动作与UIView. 但是当我尝试使用 a 时UIViewController,这会带来麻烦。

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?

有人可以建议我如何AboutViewController在单击具有翻转效果的信息按钮时显示 UIViewController (在我的应用程序中,我需要显示)?

回答by Filip Radelic

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

回答by Michael Morrison

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.

幸得faysar这里

回答by Di Wu

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

你对 Cocoa Touch 中 MVC 结构的理解存在一个根本性的缺陷,即View ControllersViews具有可比性和相似性。事实是,他们不是。

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.

回到你的具体问题,是的,动画基于视图,而不是视图控制器。但是每个视图都应该由您的视图控制器之一控制。而这个哪个视图属于哪个控制器的事情完全取决于你。在您的情况下,动画可能发生在具有 2 个不同控制器的 2 个视图之间,或者,它们也可能发生在同一控制器的 2 个视图之间。

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.

至于代码示例,我建议您查看 Xcode 的默认模板之一,Utility Application,它以简洁和标准化的方式实现了这种点击和翻转动画。

回答by taha

  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)

    }

回答by User558

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