2013-07-21 92 views
8

我想要一个UIViewController,它以右边的“幻灯片”动画出现。不像推普塞格,不像Facebook应用程序。我希望新的ViewController滑动当前窗口的顶部(而不是将它推开),但只覆盖屏幕的一部分,而让另一部分显示第一个ViewController。UIViewController半屏“抽屉滑动”动画

我曾尝试:我已经得到最接近的是通过以下创建自定义赛格瑞:

- (void)perform 
{ 
    __block UIViewController *src = (UIViewController *) self.sourceViewController; 
    __block UIViewController *dst = (UIViewController *) self.destinationViewController; 

    CATransition* transition = [CATransition animation]; 
    transition.duration = .50; 
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    transition.type = kCATransitionMoveIn; 
    transition.subtype = kCATransitionFromRight; 

    [src.navigationController.view.layer addAnimation:transition forKey:@"SwitchToView1"]; 
    [src.navigationController pushViewController:dst animated:NO]; 
} 

这实现了我要为动漫,但它涵盖了整个第一视图控制器。我该如何让它在某个时刻停下来,而不是覆盖整个事物?

我正在使用故事板,我这是我第一次尝试任何形式的新动画。

+0

你好,我也需要实现同样的事情,你能给我一些关于我放置这个代码的类的初步想法吗? –

回答

12

你可以尝试做它在源视图控制器,并改变帧您destnation(x轴)喜欢的东西:

- (void) perform {  
    UIViewController *dst = (UIViewController *) self.destinationViewController; 

    [dst.view setFrame:CGRectMake(160, 0, YOUR_DST_WIDTH, YOUR_DST_HEIGHT)]; 

    //your animation stuff... 

    [self addChildViewController:dst]; 
    [self.view addSubview:dst.view]; 
    [dst didMoveToParentViewController:self]; 
} 

而且应该这样做!

让我知道,如果它没有...

UPDATE !:

@CaptJak哎,遗憾的是没有为你..我写了下面的代码工作了,和它的工作原理这里没有任何问题..我把它链接到一个按钮点击..试试吧,让我知道! (PS:我也添加了动画!)。

ViewController *tlc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainViewController"]; 
[tlc.view setFrame:CGRectMake(-320, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

[self addChildViewController:tlc]; 
[self.view addSubview:tlc.view]; 
[tlc didMoveToParentViewController:self]; 

[UIView animateWithDuration:0.3 animations:^{ 
    [tlc.view setFrame:CGRectMake(-160, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
}]; 
+0

不,不起作用。我不是以编程方式创建dst VC,如果我放入我的源VC,'self.destinationViewController'不起作用。我上面的代码来自“UIStoryBoardSegue”子类(因此是源和目标控制器)。做上述也不会给我动画... – CaptJak

+0

你有什么建议吗? – CaptJak

+0

@CaptJak检查我的新更新! – Albara

5

使用Albara给出的答案,我也可以用相同的动画创建一个segue。自定义赛格如下:

- (void)perform 
{ 
    UIViewController *src = (UIViewController *)self.sourceViewController; 
    UIViewController *dst = (UIViewController *)self.destinationViewController; 

    [dst.view setFrame:CGRectMake(380, 0, dst.view.frame.size.width, dst.view.frame.size.height)]; 

    [src addChildViewController:dst]; 
    [src.view addSubview:dst.view]; 
    [dst didMoveToParentViewController:src]; 

    [UIView animateWithDuration:0.5 animations:^{ 
     [dst.view setFrame:CGRectMake(300, 0, src.view.frame.size.width, src.view.frame.size.height)]; 
    }]; 

} 

只是一个重命名的问题,真的。

+0

先生,我将如何使用uigesturerecongniton删除此视图? –

2

我刚刚创建了NDOverlayViewController来做这样的事情。实际上,它可以将一个视图控制器从具有可变偏移量,范围和动画选项的任何边缘叠加/滑动到另一个视图控制器上我创建了它作为一个实验,但也许这对别人有帮助?