简体   繁体   English

自定义交互式UIViewController过渡

[英]Custom Interactive UIViewController Transition

I would like to implement a custom interactive UIViewController transition that works as follows: 我想实现一个自定义的交互式UIViewController过渡,其工作方式如下:

  • There is a parent UIViewController that presents a full-screen child UIViewController. 有一个父UIViewController呈现了一个全屏子UIViewController。
  • If I swipe left, the child view controller gets displaced to the left and a new child view controller appears on the right. 如果向左滑动,则子视图控制器将移至左侧,而新的子视图控制器将显示在右侧。
  • If the transition finishes, the new child view controller occupies the screen and the old child view controller is removed. 如果过渡完成,则新的子视图控制器将占据屏幕,并删除旧的子视图控制器。

The interaction has to be interactive, so that the transition animation happens alongside the swipe gesture. 交互必须是交互的,以便过渡动画与滑动手势同时发生。

I have thought of two ways to implement this: 我想到了两种方法来实现此目的:

1) Use a UINavigationController and implement the UINavigationControllerDelegate protocol, setting up the animation and interaction controllers there. 1)使用UINavigationController并实现UINavigationControllerDelegate协议,在那里设置动画和交互控制器。

However, this is not a good solution because I don't want stack-based navigation, and don't want to keep a reference to the old child view controller. 但是,这不是一个好的解决方案,因为我不想基于堆栈的导航,也不想保留对旧的子视图控制器的引用。

2) Implement the UIViewControllerTransitioningDelegate protocol and use the "presentViewController:animated:completion:" method. 2)实现UIViewControllerTransitioningDelegate协议,并使用“ presentViewController:animated:completion:”方法。

However, this method should be used to present a view controller modally, not to replace a currently shown view controller with a new one. 但是,应使用此方法以模态形式显示视图控制器,而不是用新的视图控制器替换当前显示的视图控制器。

Is there a different way to do this? 有其他方法可以做到这一点吗?

You don't have to own multiple UIViewControllers to reach your target, also it's not recommended, and it'll cause you a lot of memory problems. 您不必拥有多个UIViewControllers即可达到目标,也不建议这样做,它会导致很多内存问题。

The best practice is to have a single UIViewController, and show the user a swiping effect, and change the view's data in the same time. 最佳做法是拥有一个UIViewController,向用户显示滑动效果,并同时更改视图的数据。

This code will give a cool swiping effect: 此代码将产生很酷的滑动效果:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UISwipeGestureRecognizer *recognizerRight;
    recognizerRight.delegate = self;

    recognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
    [recognizerRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.view addGestureRecognizer:recognizerRight];


    UISwipeGestureRecognizer *recognizerLeft;
    recognizerLeft.delegate = self;
    recognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeleft:)];
    [recognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.view addGestureRecognizer:recognizerLeft];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)swipeleft:(UISwipeGestureRecognizer *)swipeGesture
{
    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setDuration:0.50];
    [animation setTimingFunction:
     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.view.layer addAnimation:animation forKey:kCATransition];

    [self changeViewContent];
}

-(void)swipeRight:(UISwipeGestureRecognizer *)swipeGesture
{
    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromLeft];
    [animation setDuration:0.40];
    [animation setTimingFunction:
     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.view.layer addAnimation:animation forKey:kCATransition];

    [self changeViewContent];
}

-(void)changeViewContent
{
    // change the view content here
}

Also, if the views you want to swap between them has a totally different UI, then you can use a UITableViewController and change the table cells with every swipe to have the output you want. 另外,如果要在它们之间交换的视图具有完全不同的UI,则可以使用UITableViewController并在每次滑动时更改表单元格以获取所需的输出。

I hope that's helping you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM