UIViewController半屏幕"抽屉式"滑动动画

8
我正在尝试创建一个UIViewController,它可以从右侧以“滑动”的动画出现。不像Push Segue,也不像Facebook应用程序。我希望新的ViewController可以在当前ViewController的上方滑动(而不是将其推开),但只覆盖屏幕的一部分,留下另一部分显示第一个ViewController。 我尝试过的方法: 我最接近的方法是创建一个自定义Segue,其中包括以下内容:
- (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];
}

这样做可以实现我想要的动画效果,但是它覆盖了整个第一个视图控制器。我该如何让它在某个特定点停止并且不覆盖整个视图控制器呢?
我正在使用Storyboards,并且这是我第一次尝试任何新的动画效果。

你好,我也需要实现同样的功能,请问您能否给我一些初始想法,我应该把这段代码放在哪个类中? - sandeep tomar
3个回答

12
你可以尝试在源视图控制器中执行此操作,并更改目标的框架(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]; 
}

好的,就这样吧!

如果有问题,请告诉我...

更新!:

@CaptJak 嘿,很抱歉那对你没用..我写了下面的代码,在这里没有任何问题..我将它链接到一个按钮点击事件上..试试看,然后让我知道!(附注:我还添加了动画效果!)。

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

不行,不起作用。我没有通过编程方式创建dst VC,如果我把self.destinationViewController放在我的源VC中也不起作用。我上面的代码来自于一个UIStoryBoardSegue子类(因此有源和目标视图控制器)。这样做也不会给我动画效果... - CaptJak
你还有什么其他建议吗? - CaptJak
希望您不介意我问,有没有一种方法可以将这个动画和演示转换成Segue?我想在不同的场景中再次使用它,并通过prepareForSegue方法传递数据。 - CaptJak
@CaptJak 不,完全不是!嗯,我认为没有简单的方法可以做到...至少我不知道。但是如果我要做的话,我会选择像https://github.com/John-Lluch/SWRevealViewController这样的东西,并稍微调整一下。它与Facebook中的那个相同,但我相信你可以修改它。希望你能让它工作!祝一切顺利! - Albara
我们能否用新视图覆盖导航栏? - Chanchal Raj
显示剩余3条评论

5

使用Albara提供的答案,我也能够创建一个具有相同动画效果的segue。自定义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)];
    }];

}

只是重命名的问题,实际上。

先生,我该如何使用UIGestureRecognizer删除此视图? - sandeep tomar

2

我刚刚创建了NDOverlayViewController,可以实现类似于此功能。实际上,它可以在任何边缘上使用可变的偏移量、范围和动画选项将一个视图控制器叠加/滑动到另一个视图控制器之上。我将其创建为一次实验,但也许对某些人有帮助?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接