视图控制器的转换 - 从左到右 [英] Transition of view controllers - from left to right

查看:95
本文介绍了视图控制器的转换 - 从左到右的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用导航控制器,我正在使用故事板。

I am NOT using navigation controller, and I am using storyboards.

我必须从1个视图控制器转换到其他视图,我正在使用segue。

I have to make a transition from 1 view controller to other, for which I am using segue.

现在我将segue样式设置为Custom,并在相应的类中重写 perform 方法。

Now I set the segue style to Custom, and in the corresponding class, I override the perform method.

-(void)perform
{
    UIViewController *sourceViewController = [self sourceViewController];
    UIViewController *destinationViewController = [self destinationViewController];

    CATransition *transition = [CATransition animation];
    transition.duration = 0.25;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;

    [sourceViewController.view.layer addAnimation:transition forKey:kCATransition];animated:NO];
    [sourceViewController presentViewController:destinationViewController animated:YES completion:nil];
}

但是目的地视图控制器还有一个属性 modalTransitionStyle

But there is also a property of the destination view controller modalTransitionStyle.

所以现在,源VC从左向右移动就好像它被推动一样,但我想表明它被目标视图控制器推送。相反,目标VC在默认情况下执行覆盖垂直,并且 modalTransitionStyle 属性只有四个选项可用,但这些都不适用于我。

So Right now, the source VC goes from left to right as if it is being pushed, but I want to show that it is being pushed by the destination view controller. Instead, the destination VC does a 'Cover Vertical' by default, and there are only four options available for the modalTransitionStyle property, none of which is working for me.

我认为要使它工作,这个动画应该添加到一些超级视图层,一个超级视图,从中可以看到两个视图控制器。但是没有这样的超级视图..

I think to make it work, this animation should be added to some superview layer, a superview from which both view controllers have been presented. But there is no such superview..

推荐答案

通常你会将一个storyboardId给一个destinationController并从sourceViewController这样调用它:

Usually you would give a storyboardId to the destinationController and call it like this from the sourceViewController:

//push next view
UIStoryboard *storyboard = self.storyboard;
YourViewControllerClass *destVC = [storyboard instantiateViewControllerWithIdentifier:@"StoryboardID"];
[self.navigationController pushViewController:destVC animated:YES];

您可以选择手动执行此操作:

Optionally, you can do it manually like this:

    // Get the views.
    UIView * fromView = sourceViewController.view;
    UIView * toView = destinationViewController.view;

    // Get the size of the view area.
    CGRect viewSize = fromView.frame;

    // Add the toView to the fromView
    [fromView.superview addSubview:toView];

    // Position it off screen.
    toView.frame = CGRectMake( 320 , viewSize.origin.y, 320, viewSize.size.height);

    [UIView animateWithDuration:0.4 animations:
     ^{
         // Animate the views on and off the screen. This will appear to slide.
         fromView.frame =CGRectMake( -320 , viewSize.origin.y, 320, viewSize.size.height);
         toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height);
     }
                     completion:^(BOOL finished)
     {
         if (finished)
         {
             // Remove the old view from its parent.
             [fromView removeFromSuperview];

             //I use it to have navigationnBar and TabBar at the same time
             //self.tabBarController.selectedIndex = indexPath.row+1;
         }
     }];

** 编辑 **

反函数(类似于导航控制器中的后退按钮):

Inverse function (similar to the back button in the navigation controller):

// Get the views.
UIView * fromView = fromViewController.view;
UIView * toView = destViewController.view;

// Get the size of the view area.
CGRect viewSize = fromView.frame;

// Add the to view to the tab bar view.
[fromView.superview addSubview:toView];

// Position it off screen.
toView.frame = CGRectMake( -320 , viewSize.origin.y, 320, viewSize.size.height);

[UIView animateWithDuration:0.4 animations:
 ^{
     // Animate the views on and off the screen. This will appear to slide.
     fromView.frame =CGRectMake( 320 , viewSize.origin.y, 320, viewSize.size.height);
     toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height);
 }
                 completion:^(BOOL finished)
 {
     if (finished)
     {
         // Remove the old view from the tabbar view.
         [fromView removeFromSuperview];
     }
 }];

这篇关于视图控制器的转换 - 从左到右的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆