updateInteractiveTransition

updateInteractiveTransition

本文介绍了使用 UIViewControllerInteractiveTransitioning 委托协议实现自定义 UIViewController 交互式转换的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何创建一个实现 UIViewControllerInteractiveTransitioning 协议的 NSObject 子类的简洁示例感兴趣,以管理两个 UIViewControllers.理想情况下响应滑动手势.类似于现在带有 UINavigationController 的 iOS7 默认交互式滑动,但这是一个自定义/手动实现示例.

I'm interested in a concise example of how to create an NSObject subclass that implements the UIViewControllerInteractiveTransitioning protocol to manage a custom interactive transition between two UIViewControllers. Ideally in response to a swipe gesture. Something akin to the iOS7 default interactive swipe that now comes with UINavigationController, but a custom/manual implementation example of this.

我已阅读文档:

并查看了其他地方的一些示例:

And looked at a few examples elsewhere:

  • 一个
  • 两个
  • 三个
  • 四个(我设置了这个,但更多的是关于 UIViewController containment 和手动实现这些转换而不是 UIViewControllerInteractiveTransitioning
  • one
  • two
  • three
  • four (I set this up but it's more about UIViewController containment and manual implementation of these transitions rather than UIViewControllerInteractiveTransitioning

文档相当清晰,但没有参考示例代码.这些示例还有一些不足之处(关于各个部分如何联系在一起的未回答问题).

The docs are fairly clear but dont reference sample code. And the examples leave a little to be desired (unanswered questions about how the various pieces are tied together).

所以我的问题是:

  1. 有人可以帮助填写有关如何将手势(例如滑动)与实现 UIViewControllerInteractiveTransitioning 协议的对象相关联的空白吗?
  2. 实现 UIViewControllerInteractiveTransitioning 协议的对象和实现 UIViewControllerAnimatedTransitioning 协议的对象之间有什么关系?似乎您必须同时拥有两者才能触发交互式转换...
  1. Can someone help fill in the blanks about how to tie a gesture (e.g. a swipe) to the object that implements the UIViewControllerInteractiveTransitioning protocol?
  2. What is the relationship between object implementing the UIViewControllerInteractiveTransitioning protocol and that implementing the UIViewControllerAnimatedTransitioning protocol? Seems like you must have both to trigger interactive transitions...

提前致谢...

推荐答案

1) 将手势绑定到 UIViewControllerInteractiveTransitioning 对象的最简单方法是使其成为UIPercentDrivenInteractiveTransition.然后在实现手势处理程序的地方,调用 updateInteractiveTransition: 此处是一个带有代码的示例:

1) The easiest way to tie a gesture to the UIViewControllerInteractiveTransitioning object, is making it subclass of UIPercentDrivenInteractiveTransition. Then where you implement the gesture handler, you call updateInteractiveTransition: here an example with code:

-(void)handlePinch:(UIPinchGestureRecognizer *)pinch {

    CGFloat scale = pinch.scale;
    switch (pinch.state) {
      case UIGestureRecognizerStateBegan: {
          _startScale = scale;
          self.interactive = YES;
          [self.navigationController popViewControllerAnimated:YES];
          break;
      }
      case UIGestureRecognizerStateChanged: {
          CGFloat percent = (1.0 - scale/_startScale);
          [self updateInteractiveTransition:(percent < 0.0) ? 0.0 : percent];
          break;
      }
      case UIGestureRecognizerStateEnded: {
          CGFloat percent = (1.0 - scale/_startScale);
          BOOL cancelled = ([pinch velocity] < 5.0 && percent <= 0.3);
          if (cancelled) [self cancelInteractiveTransition];
          else [self finishInteractiveTransition];
          break;
      }
      case UIGestureRecognizerStateCancelled: {
          CGFloat percent = (1.0 - scale/_startScale);
          BOOL cancelled = ([pinch velocity] < 5.0 && percent <= 0.3);
          if (cancelled) [self cancelInteractiveTransition];
          else [self finishInteractiveTransition];
          break;
      }
    }
}

此代码来自https://www.captechconsulting.com/blogs/ios-7-tutorial-series-custom-navigation-transitions--more

2) UIViewControllerAnimatedTransitioninganimateTransition 函数用于执行交互过渡.由于您之前对 updateInteractiveTransition 的调用,它会自动划分为关键帧".但我想,如果你实现了 UIViewControllerInteractiveTransitioningstartInteractiveTransition: 方法(所以不使用 UIPercentDrivenInteractiveTransition 子类)那么你有责任管理完全转换(不确定……抱歉,我认为文档不是很清楚).

2) The function animateTransition of UIViewControllerAnimatedTransitioning is used to perform the interactive transition. It is automatically partitioned in "keyframes" thanks to your previous call to updateInteractiveTransition. But I suppose that if you implement your startInteractiveTransition: method of UIViewControllerInteractiveTransitioning (so without using UIPercentDrivenInteractiveTransition subclass) then you are responsible to manage the fully transition (not sure about that.. sorry but the documentation in my opinion is not really clear).

这篇关于使用 UIViewControllerInteractiveTransitioning 委托协议实现自定义 UIViewController 交互式转换的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:55