如何将ViewController从左侧推到另一个上面

时间:2016-01-28 16:59:43

标签: 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

当我点击按钮我的屏幕更改时如下:

enter image description here

我以前的VIewController也在移动。

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

1 个答案:

答案 0 :(得分:1)

您可能希望考虑使用子控制器。

这是一个非常简化的代码,用于演示此概念。它添加了一个子视图控制器,并将其设置为屏幕中间的动画。这至少应该给你一个起点:

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