HCNavigationController
类似于 UINavigationController,macOS 上
如何使用
let navigationController = HCNavigationController(withFrame: NSApp.keyWindow!.frame, rootViewController: nil)
let window = NSWindow(contentViewController: self.navigationController!)
window.makeKeyAndOrderFront(nil)
// Push
let vc = TestViewController(nibName: "TestViewController", bundle: nil)
navigationController.pushViewController(vc, animated: true)
// Pop
navigationController?.popViewController(vc, animated: true)
内部结构
实际上,我们只是在根视图包含的两个子视图中进行过渡。
水平排列两个子视图。然后从右向左滑动整个视图进行推送,然后以相反方向弹出。
Push
◀─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
│ │ │ │
│ │ │ │
│ From │ │ To │
│ │ │ │
│ │ │ │
└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▶
Pop
为了使过渡流畅,我们使用 from-view 和 to-view 作为占位符,隐藏所有添加/删除操作。
并且,类似于 iOS 中的 drawViewHierarchyInRect:afterScreenUpdates
,NSView 支持类似的方法如下
extension NSView {
func snapshot() -> NSImage? {
// Returns a bitmap-representation object suitable for caching the specified portion of the view.
guard let bitmapRep = bitmapImageRepForCachingDisplay(in: bounds) else { return nil }
cacheDisplay(in: bounds, to: bitmapRep)
let image = NSImage()
image.addRepresentation(bitmapRep)
bitmapRep.size = bounds.size
return image
}
}