ios - 如何将一个 ViewController 从左侧推到另一个

标签 ios objective-c uiviewcontroller uinavigationcontroller xcode6

我想制作这样的抽屉导航: enter image description here

我看到很多第三方库。但我想自己做。

这是我正在尝试的:

   __block UIViewController *sourceViewController = self;

NavigationDrawerVC *controller = [[NavigationDrawerVC alloc] initWithNibName:@"NavigationDrawerVC" bundle:nil];


__block UIViewController *destinationController = (UIViewController*)controller;

CATransition* transition = [CATransition animation];
transition.duration = 5;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom



[sourceViewController.navigationController.view.layer addAnimation:transition
                                                            forKey:kCATransition];


[sourceViewController.navigationController pushViewController:destinationController animated:NO];

但我是这样得到 o/p 的:

enter image description here

当我点击 Button 时,我的屏幕会变成这样:

enter image description here

我以前的 VIewController 也在移动。

我想修复它。而且我也无法在某个时候停止它。

最佳答案

您可能想为此使用子 Controller 。

下面是演示此概念的非常简化的代码。它添加了一个 subview Controller 并将其动画化到屏幕中间。这至少应该给你一个起点:

UIViewController *drawerVC = [[UIViewController alloc] init];
drawerVC.view.backgroundColor = [UIColor redColor];

[self addChildViewController:drawerVC];
drawerVC.view.frame = CGRectMake(-self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:drawerVC.view];
[drawerVC didMoveToParentViewController:self];

[UIView animateWithDuration:0.3 animations:^{
    drawerVC.view.frame = CGRectOffset(drawerVC.view.frame, drawerVC.view.frame.size.width * 0.5, 0);
}];

关于ios - 如何将一个 ViewController 从左侧推到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35067471/

相关文章:

ios - 如何在 iOS 中创建混合的 "Master-Detail and Tabbar"应用程序

ios - 如何创建新收藏集?

iphone - 如何使 UISegmentedControl 像 UITabBarController 一样工作?

ios - 返回按钮有问题

swift - UICollectionView 行为与 Subview 不同

objective-c - 滚动表时向核心数据发出获取请求

ios - 无法为 IOS7 创建应用程序图标

objective-c - NSDictionary - 列出所有

iphone - cocos2d CCSprite 碰撞问题

ios - 将对 viewController 的引用传递给其他 viewController cocoa touch