在不使用导航控制器堆栈,子视图或模态控制器的情况下更改视图控制器的动画? [英] Animate change of view controllers without using navigation controller stack, subviews or modal controllers?

查看:106
本文介绍了在不使用导航控制器堆栈,子视图或模态控制器的情况下更改视图控制器的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NavigationControllers具有ViewController堆栈来管理和限制动画过渡。

NavigationControllers have ViewController stacks to manage, and limited animation transitions.

将视图控制器作为子视图添加到现有视图控制器需要将事件传递给子-view控制器,这是一个痛苦的管理,加载很少的烦恼,并且在实施时通常感觉像是一个糟糕的黑客(Apple也建议不要这样做)。

Adding a view controller as a sub-view to an existing view controller requires passing events to the sub-view controller, which is a pain to manage, loaded with little annoyances and in general feels like a bad hack when implementing (Apple also recommends against doing this).

呈现模态视图控制器再次将视图控制器放在另一个视图控制器之上,虽然它没有上面描述的事件传递问题,但它并没有真正交换视图控制器,而是堆叠它。

Presenting a modal view controller again places a view controller on top of another, and while it doesn't have the event passing problems described above, it doesn't really 'swap' the view controller, it stacks it.

故事板仅限于iOS 5,几乎是理想的,但不能在所有项目中使用。

Storyboards are limited to iOS 5, and are almost ideal, but cannot be used in all projects.

有人可以提供一个固定代码关于在没有上述限制的情况下更改视图控制器的方法的示例,并允许它们之间的动画转换?

Can someone present a SOLID CODE EXAMPLE on a way to change view controllers without the above limitations and allows for animated transitions between them?

一个关闭示例,但没有动画:
如何在没有导航控制器的情况下使用多个iOS自定义视图控制器

A close example, but no animation: How to use multiple iOS custom view controllers without a navigation controller

编辑:导航控制器使用很好,但需要动画过渡样式(不仅仅是幻灯片效果),显示的视图控制器需要完全交换(不是堆叠) )。如果第二个视图控制器必须从堆栈中删除另一个视图控制器,那么它的封装不够。

Nav Controller use is fine, but there needs to be animated transition styles (not simply the slide effects) the view controller being shown needs to be swapped completely (not stacked). If the second view controller must remove another view controller from the stack, then it's not encapsulated enough.

编辑2:iOS 4应该是这个问题的基本操作系统,I应该在提到故事板(上图)时澄清。

Edit 2: iOS 4 should be the base OS for this question, I should have clarified that when mentioning storyboards (above).

推荐答案

编辑:任何有效的新答案orientation。
原始答案仅在界面处于纵向时才有效。这是b / c视图转换动画,用于替换具有不同视图的视图必须在视图至少比添加到窗口的第一个视图低一级的情况下发生(例如 window.rootViewController.view.anotherView )。

我实现了一个简单的容器类,我称之为 TransitionController 。您可以在 https://gist.github.com/1394947 找到它。

I've implemented a simple container class I called TransitionController. You can find it at https://gist.github.com/1394947.

顺便说一句,我更喜欢在单独的类b / c中实现它更容易重用。如果你不想这样,你可以直接在你的app委托中实现相同的逻辑,而不需要 TransitionController 类。但是你需要的逻辑是相同的。

As an aside, I prefer the implementation in a separate class b/c it's easier to reuse. If you don't want that, you could simply implement the same logic directly in your app delegate eliminating the need for the TransitionController class. The logic you'd need would be the same however.

按如下方式使用它:

在您的应用代理中

// add a property for the TransitionController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    MyViewController *vc = [[MyViewContoller alloc] init...];
    self.transitionController = [[TransitionController alloc] initWithViewController:vc];
    self.window.rootViewController = self.transitionController;
    [self.window makeKeyAndVisible];
    return YES;
}

从任何视图控制器转换到新视图控制器

- (IBAction)flipToView
{
    anotherViewController *vc = [[AnotherViewController alloc] init...];
    MyAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.transitionController transitionToViewController:vc withOptions:UIViewAnimationOptionTransitionFlipFromRight];
}

编辑:以下原始答案 - 仅适用于肖像方向

Original Answer below - only works for portait orientation

我对此示例做了以下假设:

I made the following assumptions for this example:


  1. 您有一个视图控制器被指定为窗口的 rootViewController

当您切换到新视图时,您希望将当前viewController替换为拥有新视图的viewController。在任何时候,只有当前的viewController是活着的(例如,分配)。

When you switch to a new view you want to replace the current viewController with the viewController owning the new view. At any time, only the current viewController is alive (e.g. alloc'ed).

代码可以很容易地修改以不同的方式工作,关键点是动画过渡和单视图控制器。确保在将其分配给 window.rootViewController 之外的任何地方都不保留视图控制器。

The code can be easily modified to work differently, the key point is the animated transition and the single view controller. Make sure you don't retain a view controller anywhere outside of assigning it to window.rootViewController.

代码转换为app delegate

- (void)transitionToViewController:(UIViewController *)viewController
                    withTransition:(UIViewAnimationOptions)transition
{
    [UIView transitionFromView:self.window.rootViewController.view
                        toView:viewController.view
                      duration:0.65f
                       options:transition
                    completion:^(BOOL finished){
                        self.window.rootViewController = viewController;
                    }];
}

在视图控制器中使用示例

- (IBAction)flipToNextView
{
    AnotherViewController *anotherVC = [[AnotherVC alloc] init...];
    MyAppDelegate *appDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate transitionToViewController:anotherVC
                             withTransition:UIViewAnimationOptionTransitionFlipFromRight];
}

这篇关于在不使用导航控制器堆栈,子视图或模态控制器的情况下更改视图控制器的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆