使用UIViewControllerInteractiveTransitioning委托协议实现自定义UIViewController交互式转换的正确方法 [英] Proper way to implement a custom UIViewController interactive transition using UIViewControllerInteractiveTransitioning Delegate Protocol

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

问题描述

我对如何创建一个实现 UIViewControllerInteractiveTransitioning 协议的 NSObject 子类的简明示例感兴趣管理两个 UIViewController 之间的自定义交互式转换。理想情况下响应轻扫手势。类似于iOS7默认交互式滑动,现在附带 UINavigationController ,但这是一个自定义/手动实现示例。

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.

我已阅读文档:

  • UIViewControllerContextTransitioning
  • UIPercentDrivenInteractiveTransition
  • UIViewControllerInteractiveTransitioning
  • UIViewControllerAnimatedTransitioning

并查看其他地方的几个例子:

And looked at a few examples elsewhere:


  • 一个

  • 两个

  • 三个

  • 四个(我设置了它,但更多关于 UIViewController 包含并手动实现这些转换而不是 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)函数 animateTransition of UIViewControllerAnimatedTransitioning 用于执行交互式转换。由于您之前调用 updateInteractiveTransition ,它会自动在关键帧中进行分区。但我想如果你实现 startInteractiveTransition: 的方法> UIViewControllerInteractiveTransitioning (所以不使用 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交互式转换的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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