ToastCollectionView
⭐️ 它做些什么?
ToastCollectionView 是一个 Swift 的 UICollectionView
,在滚动时推送并显示视图,类似于从烤面包机中跳出的吐司
如果您希望显示有关单元格内容的信息,则很有用
📲 示例
要运行示例项目,请克隆仓库,然后首先在 Example 目录中运行 pod install
📋 要求
ToastCollectionView 需要 iOS 9 和 Swift 4
📦 安装
ToastCollectionView 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'ToastCollectionViewCell'
😏 用法
1. 创建一个 'Toast' 视图,您的单元格将在滚动时显示
创建一个从 UIView
类继承的类,使用您想要的形状和设计...
class MyToastView: UIView {
// Your code here
}
2. 创建 ToastCollectionView 并替换您的当前 UICollectionView
-
实例化一个
ToastCollectionView
-
实现
dataSource
和delegate
设置Toast视图偏移量
-
maxPositionForComponent
是你希望当滚动时Toast可以移动到的最高位置 -
offsetToComponent
是UICollectionView与每个Toast之间的偏移量,用于触发移动
示例
class ViewController: UIViewController {
var collectionView: ToastCollectionView?
override func viewDidLoad() {
super.viewDidLoad()
// Instanciate the CollectionView
let collectionViewLayout = UICollectionViewLayout()
self.collectionView = ToastCollectionView(frame: CGRect, collectionViewLayout: collectionViewLayout)
// Set the offsets
self.collectionView.maxPositionForComponent = 75.0
self.collectionView.offsetToComponent = 30.0
// Set the delegates
self.collectionView.delegate = self
self.collectionView.dataSource = self
}
}
3. 创建一个继承自ToastCollectionViewCell的UICollectionViewCell
重写toastViewForCell
方法,并返回你的Toast视图
override func toastViewForCell() -> UIView? {
// Return the Toast view with the size you want
return MyToastView(frame: CGRect(x: 0, y: 0, width: 150, height: 50))
}
如果你希望在Toast达到最大垂直位置时扩展Toast视图,重写widthForExpandedToastView
方法并返回新的宽度
override func widthForExpandedToastView() -> CGFloat? {
return 190.0
}
扩展功能
ToastCollectionViewCell
提供了一个preRaiseToastView(toPosition: CGFloat)
方法,允许你强制提升某些Toast视图。例如,当展示CollectionView时,第一个出现的单元格可以避免只在用户开始滚动时弹出。
代理
通过实现ToastCollectionViewCellDelegate
,当任何Toast视图到达顶部位置(即它们已经被完全提升)时,你可以被通知。
然后你可以在给定的Toast视图上触发一个操作。
示例
class ExampleCell: ToastCollectionViewCell, ToastCollectionViewCellDelegate {
func onToastFullyRaised(toast: UIView) {
let toast = toast as! MyToastView
toast.doSomething()
}
}
👱 作者
Oscar Gotting (@Scaraux)
🚔 许可证
ToastCollectionViewCell 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。