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

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

问题描述

NavigationControllers 有 ViewController 堆栈来管理和有限的动画转换.

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

将视图控制器作为子视图添加到现有视图控制器需要将事件传递给子视图控制器,这很难管理,几乎没有烦恼,并且在实现时通常感觉像一个糟糕的黑客(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.

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

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 应该是这个问题的基本操作系统,我应该在提到故事板时(上文)澄清这一点.

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

推荐答案

适用于任何方向的新答案.原始答案仅在界面处于纵向时才有效.这是 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 中实现它更容易重用.如果您不想这样,您可以直接在您的应用程序委托中实现相同的逻辑,从而无需 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];
}

下面的原始答案 - 仅适用于肖像方向

我对这个例子做了以下假设:

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.

用于在应用委托中设置动画过渡的代码

- (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天全站免登陆