gpt4 book ai didi

ios - 在自定义 UIViewController 容器中管理子项和在子项之间导航的正确方法?

转载 作者:行者123 更新时间:2023-11-28 06:12:59 27 4
gpt4 key购买 nike

我一直使用标准的 UIKit UIViewController 容器,比如 UIPageViewController 来满足我的容器需求,但我想尝试创建自定义容器。但有一些具体的事情我不确定。如果没有 Storyboard,这将完全是程序化的(因此,没有 segue)。

  1. 我希望容器容纳(为了这个示例)4 个 UIViewController,它们将成为 UI 的 4 个主要部分。在每个部分中,将有一个 UINavigationController 来处理其部分中的堆栈。因此,这 4 个 UIViewController 应该作为子项永久添加到容器中(永远不要删除),对吗?推理:以便用户在离开和返回该部分时不会丢失他/她在导航堆栈中的位置。

  2. 我想在这 4 个 View Controller 之间实现自定义动画转换,这些 View Controller 将使用平移手势将它们拖入和拖出 View 。例如,在过去,使用 UINavigationController 时,自定义动画对象会有效地覆盖 pushViewController(),但我会在这里有效地覆盖什么?

    <

如果我错了请纠正我,但根据 Apple 的说法,据我所知, View Controller 可以被推送或“呈现”。我说“presented”是因为“presented”包括“present”(我认为这是令人困惑的地方),它以模态方式引入 UIViewController,而“show”也包含在“presented”下"umbrella,不会以模态方式引入 UIViewController,它的工作方式类似于 push(),只是它不在导航堆栈中。

最佳答案

你问:

Therefore, these 4 UIViewController's should be permanently added as children to the container (and never removed), correct?

作为一个小的改进,您可以以即时方式加载它们,仅在您真正需要它们时加载它们。这避免了由于加载许多可能还不可见(如果有的话)的 View Controller 而导致的任何潜在延迟。但是,可以肯定的是,一旦加载,您可以将它们保存在内存中。这是 Apple 自己的容器 View Controller 所采用的模式。

... show ... does not bring in a UIViewController modally, it works in some way like push() except that it's not in a navigation stack.

show 方法将导航 View Controller 层次结构,确定它在哪个上下文中找到自己并相应地进行转换。如果您在导航 Controller 或 Split View Controller 中,它将执行适当的转换。但是,如果您不在其中一个 Controller 中,它执行模式演示。查看show documentation .

I want to implement custom animation transitions between these 4 view controllers that will use pan gestures to drag them in and out of view...

我建议引用 Custom Container View Controller Transitions ,其中概述了他们如何使用 custom transition结合自己的图案view controller containment .

如果这变得太笨重,您可能只是“守旧派”,只是使用手势为包含的 View Controller 进出动画 View ,执行所有适当的 View Controller 包含相关调用。它没有那么优雅,但我怀疑这可能更容易。


如果您手动执行此操作(更容易),唯一的技巧是确保执行适当的外观和 View Controller 包含调用。因此,在下面的示例中,我们从添加为容器 View Controller 的 subview Controller 的四个 subview Controller 之一开始,这显示了如何使用手势将那个 subview Controller 替换为另一个 subview Controller 。这使 View Controller 层次结构与 View 层次结构保持同步,并确保子级获得适当的外观方法调用:

import UIKit
import UIKit.UIGestureRecognizerSubclass
import os.log

class ViewController: UIViewController {

lazy private var swipeFromRight: UIScreenEdgePanGestureRecognizer = {
let swipe = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handleSwipeFromRight(_:)))
swipe.edges = .right
return swipe
}()

private var nextChildController: UIViewController?
private var currentChildController: UIViewController?

override func viewDidLoad() {
super.viewDidLoad()

view.addGestureRecognizer(swipeFromRight)
}

@objc func handleSwipeFromRight(_ gesture: UIScreenEdgePanGestureRecognizer) {
let percent = -gesture.translation(in: gesture.view).x / gesture.view!.bounds.width

if gesture.state == .began {
os_log("starting gesture", type: .debug)

guard let next = nextViewController() else {
gesture.state = .cancelled
return
}

currentChildController = childViewControllers.first!
nextChildController = next
startAppearance(next, replacing: currentChildController!, in: currentChildController!.view.superview)

// prepare for gesture driven animation

next.view.frame = currentChildController!.view.frame
next.view.transform = .init(translationX: next.view.frame.width, y: 0)
} else if gesture.state == .changed {
// update animation based upon progress percent

nextChildController!.view.transform = .init(translationX: nextChildController!.view.frame.width * (1 - percent), y: 0)
} else if gesture.state == .ended {
// figure out whether we should finish the animation (if not, we'll reverse it)

let velocity = gesture.velocity(in: gesture.view).x
let shouldFinish = velocity < 0 || (velocity == 0 && percent > 0.5)

os_log("finishing gesture: shouldFinish=%@", type: .debug, shouldFinish ? "true" : "false")

let next = nextChildController!
let previous = self.currentChildController!

if shouldFinish {
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseOut, animations: {
next.view.transform = .identity
}, completion: { finished in
self.endAppearance(next, replacing: previous)
})
} else {
beginCancelAppearance(next, replacing: previous)

UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseOut, animations: {
next.view.transform = .init(translationX: next.view.frame.width, y: 0)
}, completion: { finished in
self.endCancelAppearance(next, replacing: previous)

next.view.transform = .identity
})
}
}
}

// MARK: Child appearance/containment helpers

private func startAppearance(_ appearingController: UIViewController, replacing disappearingController: UIViewController, in view: UIView? = nil) {
appearingController.beginAppearanceTransition(true, animated: true)

addChildViewController(appearingController)
view?.addSubview(appearingController.view)

disappearingController.willMove(toParentViewController: nil)
disappearingController.beginAppearanceTransition(false, animated: true)
}

private func beginCancelAppearance(_ appearingController: UIViewController, replacing disappearingController: UIViewController) {
appearingController.willMove(toParentViewController: nil)
appearingController.beginAppearanceTransition(false, animated: true)

disappearingController.willMove(toParentViewController: self)
disappearingController.beginAppearanceTransition(true, animated: true)
}

private func endCancelAppearance(_ appearingController: UIViewController, replacing disappearingController: UIViewController) {
appearingController.view.removeFromSuperview()
appearingController.removeFromParentViewController()
appearingController.endAppearanceTransition()

disappearingController.endAppearanceTransition()
}

private func endAppearance(_ appearingController: UIViewController, replacing disappearingController: UIViewController) {
appearingController.endAppearanceTransition()
appearingController.didMove(toParentViewController: self)

disappearingController.view.removeFromSuperview()
disappearingController.removeFromParentViewController()
disappearingController.endAppearanceTransition()
}

// MARK: Next/Previous child helpers

private func nextViewController() -> UIViewController? { ... }

private func previousViewController() -> UIViewController? { ... }

}

显然,用您想要的任何动画替换动画。而且,很明显,出售适合您的应用程序的 View Controller 。但它说明了需要完成的手势、外观和遏制调用的性质。

关于ios - 在自定义 UIViewController 容器中管理子项和在子项之间导航的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45942580/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com