简介
Drawer
是一个框架,它允许你轻松地将 UIViewController
嵌入抽屉并在另一个 UIViewController
之上显示。
📝 要求
- iOS 11
- Swift 4.0+
📦 安装
Carthage
github "nodes-ios/Drawer"
Cocoapods
pod 'NDrawer'
💻 使用方法
要求
- 一个要显示在其上的
UIViewController
。在以下步骤中,此ViewController
被称为backgroundViewController
。 - 一个要作为抽屉内容的
UIViewController
。在以下步骤中,此ViewController
被称为contentViewController
。
步骤
创建抽屉
首先,将您的 contentViewController
适配为 Embeddable
协议。这会将多个代理函数暴露给 contentViewController
。
extension ContentViewController: Embeddable {}
此外,还公开了一个 EmbeddableContentDelegate
的实例。这个 delegate
可以用来通过调用该实例的 handle
函数来指示抽屉执行各种任务。
handle
函数接收一个 enum
类型的 Drawer.Action
,允许执行以下操作:
layoutUpdated(config: Drawer.Configuration)
更新抽屉的布局changeState(to: MovementState)
显示/隐藏抽屉。
创建完 contentViewController
后,在您的 backgroundViewController
中初始化一个 DrawerCoordinator
的实例以初始化抽屉。
let drawer = DrawerCoordinator(contentViewController: contentVC,
backgroundViewController: self,
drawerBackgroundType: .withColor(UIColor.black.withAlphaComponent(0.5)))
显示抽屉
在您的内容视图创建完毕且您准备好显示抽屉后,创建一个 Drawer.Configuration
的实例来设置抽屉的状态和属性。
let options: [Drawer.Configuration.Key : Any] = [
.animationDuration: 0.5,
.fullHeight: maxHeight,
.minimumHeight: minHeight,
.initialState: Drawer.State.minimized,
.cornerRadius: Drawer.Configuration.CornerRadius(fullSize: 20,
minimized: 0)
]
let contentConfiguration = Drawer.Configuration(options: options,
dismissCompleteCallback: nil)
通过调用带有一个类型为 Drawer.Action
的 enum
作为参数的 handle
函数来管理与 EmbeddableContentDelegate
的通信。最后,调用 EmbeddableContentDelegate
的 handle
函数来更新抽屉的布局到新的 Configuration
。
embedDelegate?.handle(action: .layoutUpdated(config: contentConfiguration))
展开和折叠抽屉
要程序化地展开和折叠抽屉,请调用带有包含抽屉应过渡到的状态的 changeState
操作的 EmbeddableContentDelegate
handle
函数。
embedDelegate?.handle(action: .changeState(to: .fullScreen))
示例项目
欲了解更多信息,请参阅此仓库中包含的示例项目。
👥 鸣谢
由
📄 许可
Drawer 在 MIT 许可证下可用。更多信息请参阅 LICENSE 文件。