ComponentSwift 将 ComponentKit 带到 Swift 中。
ComponentKit 是 Facebook 开发的受 React 启发的 iOS 原生视图框架。它引入了一个新的抽象层(组件),以自动处理视图重用,同时提供构建平滑列表视图的能力。
ComponentSwift 是 ComponentKit 的 Objective-C 封装,经过针对 Swift 的优化。ComponentSwift 被设计为 ComponentKit 的子集,实现了最常用的功能。
示例
class CellComponent: CompositeComponent {
init?(text: String) {
super.init(
view:
ViewConfiguration(
attributes:
.backgroundColor(.gray),
.tapGesture(#selector(didTapSelf))
),
component:
getAnotherComponent(text) //
}
// ...
或使用 pod try ComponentSwift
尝试真实演示
支持的功能
支持
- 组件
- 几乎所有内置组件。
- 能够将现有的 CKComponent 封装在自己的组件中
- 数据源
- CollectionViewTransactionalDatasource
- TableViewTransactionalDatasource(非官方)
- 宿主视图
- 响应链和动作
- 作用域和状态
- 动画
不支持
- 组件控制器和StatefulViewComponent
- 关于viewContext、mount的内容
-
computeLayoutThatFits:
覆盖 - ...
处理遗留代码
在ComponentSwift中使用时,遗留的CKComponent类继续在swift环境中工作。提供了类和方法来方便地封装遗留组件。ComponentKit中的每个属性在ComponentSwift中都有一个等效属性。
继承CSCompositeComponent
,并在其实现中使用initWithCKComponent
。使用此功能前需要导入ComponentSubclass.h
。有关更多内容,请参考示例项目中的WrapExisted
。
简化内容
ComponentKit是通过objc++实现的并用于 (!$>),这非常酷。因为它与swift不兼容,因为swift不支持objc++,所以出现了ComponentSwift。
实现了一个objc包装器,用于桥接componentKit到swift。它为componentKit的API中的每个类型创建了一个等效的objc版本,对c++结构/类的objc类进行包装,同时在实现中隐藏c++头文件。它不深入处理componentKit。在最简单的情况下,除了objc层的构造之外,运行时代码几乎与直接使用componentKit相同,所以它是安全的。
它对swift的桥接口进行了优化,包括标记nullability、添加默认参数和swift-only api。C++在componentKit中的聚合初始化非常表达。ComponentSwift尽力使用默认参数和ExpressibleByXXXLiteral来模拟api。至少我们在这里有了代码补全。
安装
pod "ComponentSwift"
此库与ComponentKit v0.15.1版本一同发布,该版本的最新版本已经不再支持cocoapods。对于版本0.20,请前往0.20
分支。
许可证
ComponentSwift受MIT许可证的许可。