代码之家  ›  专栏  ›  技术社区  ›  JoeVictor

使用ContainerViewController,如何在子ViewController之间添加交互式动画?

  •  1
  • JoeVictor  · 技术社区  · 7 年前

    我正在尝试创建一个定制的 NavigationViewController 其中导航栏是顶部的一个可切换栏,用于控制过渡(想想 UITabBarController 但使用滑动手势而不是按钮)。 Here is a quick mockup. I apologize for the crudeness

    scrollViewDidScroll 方法所以我把标题栏部分装配好了。我现在需要在 滚动视图DidScroll 方法

    我已经查过了 Apple Documentation 关于如何将视图控制器嵌入彼此内部,但它并没有真正帮助解释如何做到这一点。文档中提到调用 addChildViewController: 方法和一系列其他方法,如果我想让viewControllers立即消失并重新出现,这些方法很好,但在这个场景中,我发现很难以交互方式完成。

    我是否创建当前VC的快照,然后将其移动?但我到底在动什么?例如,在图片中,我是否将整个RedVC移动到左侧?但是,如果BlueVC开始从帧外移动,我如何获取它的快照呢?如果我想在BlueVC中异步加载图像怎么办?在BlueVC捕捉到位之前,我是否必须使用带有占位符的占位符快照?

    这一切变得如此复杂。。。我单独做过类似的事情(快照、自定义VC转换等)但在这样的情况下,我不太愿意把它们都放在一起。我相信只要有足够的时间,我一定能找到办法, 但我想学习 什么是最好、最干净的方法。

    非常感谢您的帮助!谢谢

    接受@MilanNosÃľ的回答后编辑:

    我最终使用了他的框架 this repo . 它还没有交互功能,但我可以利用他们为我所做的一切来解决其余的问题。我希望我可以在这里发布完整的代码,但这不是很实际。回购协议将无限期延长,以供未来旅行者使用。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Milan Nosáľ    7 年前

    我要无耻地宣传我自己的开源项目,在这个项目中,我正在处理类似的任务- InteractiveTransitioningContainer . 它的目标是为实现自定义容器准备一个框架,允许在其子控制器之间进行交互式转换。虽然这可能不是问题的直接答案,但我花了很多时间尝试为子控制器提供与标准容器相同的环境,例如,确保 view(Will|Did)(A|Disa)ppear 回调按正确的顺序调用-我必须尝试 UINavigationController 分析其行为等。

    因此,如果没有其他东西,也许你会在那里找到一些灵感/知识。

        2
  •  0
  •   Shehata Gamal    7 年前

    您真正需要的是创建自定义序列,以便从当前VC移动到目标VC,并覆盖这样的执行方法,在这个代码段中,当前VC向上移动,目标从下至上设置动画,您可以随意更改任何方式

        override func perform() {
            // Assign the source and destination views to local variables.
            var firstVCView = self.sourceViewController.view as UIView!
            var secondVCView = self.destinationViewController.view as UIView!
    
            // Get the screen width and height.
            let screenWidth = UIScreen.mainScreen().bounds.size.width
            let screenHeight = UIScreen.mainScreen().bounds.size.height
    
            // Specify the initial position of the destination view.
            secondVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)
    
            // Access the app's key window and insert the destination view above the current (source) one.
            let window = UIApplication.sharedApplication().keyWindow
            window?.insertSubview(secondVCView, aboveSubview: firstVCView)
    
            // Animate the transition.
            UIView.animateWithDuration(0.4, animations: { () -> Void in
                firstVCView.frame = CGRectOffset(firstVCView.frame, 0.0, -screenHeight)
                secondVCView.frame = CGRectOffset(secondVCView.frame, 0.0, -screenHeight)
    
                }) { (Finished) -> Void in
                    self.sourceViewController.presentViewController(self.destinationViewController as UIViewController,
                        animated: false,
                        completion: nil)
            }
    
        }
    

    此处显示更多信息 custom-segue-animations

        3
  •  0
  •   JoeVictor    7 年前

    我最初选择了米兰的答案作为这个问题的最终答案。但是,在我过去几周尝试使用它之后 很多 在他的帮助下,我们都同意很难将他的框架集成到可用于此任务的格式中。我现在正在使用 UIScrollView 对于下面的ViewControllers,并让 HeaderView 上方(带有标签的对象)控制下方ViewController的滚动视图。

    推荐文章