SwiftEmptyState
演示
为什么需要?
常见用例;
TLDR;
想象您从后端获取数据,请求成功,但是是空列表。不要让用户面对一个空白的屏幕或无聊的警报。
用法
为了更好地理解,请查看示例项目。
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
。
但是下面是主要概念。
适用于任何 UITableViewController
、UICollectionViewController
和 UIViewController
。
class TableViewController: UITableViewController {
lazy var emptyStateManager: EmptyStateManager = {
let esv = EmptyStateView(
messageText: "This is label belongs to empty state view that sits in UITableViewController's UITableView",
titleText: "EmptyState Title",
image: UIImage(named: "empty_state_image"),
buttonText: "Button",
centerYOffset: -100 // In case you want to move it to top, by default it is centered (offset = 0)
)
esv.buttonAction = { _ in
esv.messageText = "Button action works 👍🏻"
}
let manager = EmptyStateManager(
containerView: self.tableView,
emptyView: esv,
animationConfiguration: .init(animationType: .spring)
)
return manager
}()
var dataSource = (1...50).map { _ in UIColor.random } {
didSet {
self.tableView.reloadData()
self.emptyStateManager.reloadState()
}
}
// dataSource is the dataSource of tableView
}
相同的示例也适用于 UICollectionView contentView;
如果您的内容视图是 UICollectionView/UITableView
的子类
管理器知道它是否有内容,但如果你使用纯 UIView 作为内容视图
请确保您手动设置 hasContent。
manager.hasContent = {
!self.dataSource.isEmpty
}
动画
EmptyStateManager
具有默认值的animationConfiguration参数。
struct AnimationConfiguration {
let animationType: AnimationType // .spring, .fromBottom, .fromLeft, .fromTop, .fromRight
let animationDuration: TimeInterval
let subItemDelayConstant: Double // image, titleLabel, messageLabel, button. Except .spring animation
let springDamping: CGFloat // .spring animation case
let initialVelocity: CGFloat
let options: UIView.AnimationOptions
}
let manager = EmptyStateManager(
containerView: ***,
emptyView: ***,
animationConfiguration: .init(animationType: .spring)
)
定制
如果您想自定义EmptyStateView
只需在自定义UIView类中实现IEmptyStateView
协议。
要求
安装
SwiftEmptyState可以通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile中
pod 'SwiftEmptyState'
作者
eneskaraosman, [email protected]
许可证
SwiftEmptyState遵循MIT许可证。有关更多信息,请参阅LICENSE文件。