美文网首页
iOS - 交互转场

iOS - 交互转场

作者: 温柔vs先生 | 来源:发表于2020-09-01 09:54 被阅读0次
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController

                          interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) 

官方文档:

Implement this delegate method when you want to provide a custom, interactive transition between view controllers as they are added to or removed from the navigation stack. 

当你想提供一个自定义的、交互的视图控制器之间的过渡,当他们被添加到或从导航堆栈中移除时,执行此委托方法。该方法的返回值是一个实现了UIViewControllerInteractiveTransitioning协议的对象,这个对象不需要我们自己实现,系统的UIPercentDrivenInteractiveTransition对其进行了封装,我们只需返回一个该类的实例就可以。一起来看一下这个类
官方文档:
A percent-driven interactive transition object drives the custom animation between the disappearance of one view controller and the appearance of another. It relies on a transition animator delegate—a custom object that adopts the UIViewControllerAnimatorTransitioning protocol—to set up and perform the animations.

To use this concrete class, return an instance of it from your view controller delegate when asked for an interactive transition controller. As user events arrive that would affect the progress of a transition, call the updateInteractiveTransition:, cancelInteractiveTransition, and finishInteractiveTransition methods to reflect the current progress. For example, you might call these methods from a gesture recognizer to reflect how much of the gesture is completed.
一个UIPercentDrivenInteractiveTransition交互式转场对象驱动在视图切换时自定义动画的展现。它依赖于一个过渡动画代理--一个自定义的遵循UIViewControllerAnimatorTransitioning 协议的对象,以此来设置和展现动画。要使用这个具体的类,当被要求为一个交互式的转换控制器时,返回一个实例控制器委托的实例。当用户到达的事件会影响过渡进程,调用updateinteractivetransition:,cancelinteractivetransition,和finishinteractivetransition方法反映当前进展。例如,你可能这些方法从一个手势识别来电反映多少的手势完成。

一起来看一下这三个过渡进程的方法
- (void)updateInteractiveTransition:(CGFloat)percentComplete

官方文档:While tracking user events, your code should call this method regularly to update the current progress toward completing the transition. If, during tracking, the interactions cross a threshold that you consider signifies the completion or cancellation of the transition, stop tracking events and call the finishInteractiveTransition or cancelInteractiveTransition method.
在跟踪用户事件时,您的代码应该定期调用此方法来更新当前的进展,以完成该转换。如果,在跟踪过程中,相互作用的跨出门槛,你认为标志着过渡完成或取消,停止跟踪事件并调用finishinteractivetransition或cancelinteractivetransition方法

- (void)cancelInteractiveTransition
While tracking user interactions, your gesture recognizer or event-handling code would call this method when interactions suggest that the user wants to cancel or abort the view controller transition. For example, if the user reverses the swipe direction and then touch events end, suggesting that the user decided against the transition, you would call this method.

在跟踪用户的交互,你的手势识别或事件处理代码将调用此方法时的相互作用表明,用户想取消或中止的视图控制器的过渡。例如,如果用户反转了滑动方向,然后触摸事件结束,提示用户决定不改变,您将调用此方法。

- (void)finishInteractiveTransition
While tracking user interactions, your gesture recognizer or event-handling code should call this methods when the interactions suggest that the transition is now complete. For example, if the user swipes a finger, and the touch events indicate that the swipe distance crossed the threshold needed to complete the gesture, call this method when the corresponding touch events end to let the system know that it can now complete the transition.
在跟踪用户的交互,你的手势识别或事件处理代码应调用此方法时的相互作用表明,过渡已经完成。例如,如果用户使用手指和触摸事件表明,刷卡距离交叉所需要完成的动作阈值时,调用此方法时,相应的触摸事件最终让系统知道它现在可以完成转型。

那么现在,思路清晰了。1。在FromVC中实现UINavigationController的代理方法 2.为控制器注册一个手势 3、在手势的handler方法中,根据手势来调用以上三个方法,实现交互式控制动画进程

在FromVC中实现代理方法

- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController

                                   interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController{

    return self.interactionController;

}

self.interactionController的声明

@property(nonatomic,strong) UIPercentDrivenInteractiveTransition * interactionController;

注册手势

UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(didClickPanGestureRecognizer:)];
[self.navigationController.view addGestureRecognizer:panRecognizer];

手势handler实现并控制动画进程

-(void)didClickPanGestureRecognizer:(UIPanGestureRecognizer *)recognizer{


   UIView * view = self.view;

    if (recognizer.state == UIGestureRecognizerStateBegan) {

        if (self.navigationController.viewControllers.count == 2) {

            self.interactionController = [[UIPercentDrivenInteractiveTransition alloc]init];

            [self.navigationController popViewControllerAnimated:YES];

        }

    }else if (recognizer.state == UIGestureRecognizerStateChanged){

        CGPoint translation = [recognizer translationInView:view];

        CGFloat distance = translation.x/CGRectGetWidth(view.bounds);

        if (distance > 0) {

            [self.interactionController updateInteractiveTransition:distance];

        }

    }else if (recognizer.state == UIGestureRecognizerStateEnded){

        CGPoint translation = [recognizer translationInView:view];

        CGFloat distance = fabs(translation.x/CGRectGetWidth(view.bounds));

        

        if (distance > 0.5) {

            [self.interactionController finishInteractiveTransition];

        }else{

            [self.interactionController cancelInteractiveTransition];

        }//       self.interactionController一定要设置为nil,否则你可能在你不想返回的时候返回一个interactionController

        self.interactionController = nil;

    }}

参考地址:iOS进阶之旅-可交互式转场动画

相关文章

网友评论

      本文标题:iOS - 交互转场

      本文链接:https://www.haomeiwen.com/subject/fqtazhtx.html