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

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

问题描述

我没有使用导航控制器,我使用的是故事板.

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 在默认情况下会执行Cover Vertical",并且 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.

我认为要让它工作,这个动画应该被添加到一些超级视图层,一个超级视图,两个视图控制器都从其中呈现出来.但是没有这样的superview..

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..

推荐答案

通常你会给 destinationController 提供一个 storyboardId 并从 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天全站免登陆