- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直使用标准的 UIKit
UIViewController
容器,比如 UIPageViewController
来满足我的容器需求,但我想尝试创建自定义容器。但有一些具体的事情我不确定。如果没有 Storyboard,这将完全是程序化的(因此,没有 segue)。
我希望容器容纳(为了这个示例)4 个 UIViewController
,它们将成为 UI 的 4 个主要部分。在每个部分中,将有一个 UINavigationController
来处理其部分中的堆栈。因此,这 4 个 UIViewController
应该作为子项永久添加到容器中(永远不要删除),对吗?推理:以便用户在离开和返回该部分时不会丢失他/她在导航堆栈中的位置。
我想在这 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 aUIViewController
modally, it works in some way likepush()
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/
我希望能够在代码中布局我的 View Controller ,但可以看到界面构建器中显示的布局。 我知道我可以创建一个 UIView 子类,使其 IBDesignable,并将其分配给 View Co
我有一个父 UIViewController(MainVC)。从那里我有 2 个 segue 到 2 个 UIViewControllers:FirstVC(标识符:goFirstVC)和 Secon
我有一个导航 Controller 。第一个viewcontroller 是FirstViewController 的一种。当我在 FirstViewController 中点击一个按钮时,它会将第二
我有一个自定义的动画 UIViewController 过渡,似乎 iOS 中有一个错误会破坏横向布局。在主要动画方法中,我得到了横向和纵向 View 的混合。 (纵向 View 都是纵向的,所以没问
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我有一个 UIViewController,里面有 UITabBar。我正在尝试模仿 UITabBarController。 My question is how do I set or a UIVi
是否有任何方便的方法来确定是否正在从处于后台模式的应用加载 View ? 在 3.X 中,我将依赖 viewDidLoad 进行一些初始化等操作,但是对于 4.X 而言情况并非如此,因为您不能依赖调用
我将主视图 Controller 作为带有 UICollectionView 的 View Controller ,并且填充了照片。 一旦一个单元格被点击,一个新的 View Controller 被
我有一个简单的 UIViewController(单点触控),在 UI 上有一个按钮。现在,我的用例很简单 - 我想在单击按钮时重新加载 UIViewController。在某些线程上,我读到我只需要
我在它上面有一个 UIViewController 1 UIButton(添加为 subview ),在我按下 Button(见下面的图 1)之后,在它上面添加了另一个 UIViewControlle
尝试为 iPad 制作基于 Storyboard的应用程序。在其中,我需要从开始屏幕 (UIViewController) 过渡到主屏幕 (UISplitViewController),然后再到全屏
我想调整 UIViewController 的大小在 Storyboard中,所以我可以使用它的 UIViewController作为弹出 View 。 在大多数网站上,我可以读到这些操作是: 拖一个
我正在尝试从 XIB 或 Storyboard加载 ViewModel 绑定(bind)的 ViewController(使用 MvvmCross 5.0.6)。我的应用程序的逻辑是,该 View C
我正在构建一个锻炼应用程序。我的 TableViewController 中有一系列练习,每个单元格显示不同的练习。单元格转到 UIViewController。在 UIViewController
我正在尝试从 XIB 或 Storyboard加载 ViewModel 绑定(bind)的 ViewController(使用 MvvmCross 5.0.6)。我的应用程序的逻辑是,该 View C
我有一个 UIViewController,它有一个模式窗口,我想在整个界面上展示它,包括 UITabBar。 我的应用程序层次结构是这样的: UITabBarController (A) ->
我想显示另一个 ViewController 的缩小“预览”在另一个 ViewController .这可能吗?不需要交互,因此它基本上可以是屏幕截图,但按比例缩小。我可以通过手动截屏并将其保存为图像
我正在开发一个应用程序,其中 UIViewController ( firstViewController ) 包含一些 UILabels , 一个 UIButton , 和 UIView ( sub
我的标签栏中有五个标签栏项目。第五项是弹出ViewController。因此,当我单击该按钮时,当前 Controller 中将显示一个弹出窗口。我使用 UIViewController 作为子类来实
我正在开发 iOS 应用程序。 我的应用程序有 6 个按钮,每个按钮都有每个标签栏控件。 so Main screen is [btn1] [btn2] [btn3] [btn4] [btn5] [b
我是一名优秀的程序员,十分优秀!