ToastCollectionViewCell
⭐️ 它是做什么的?
ToastCollectionViewCell 是一个 Swift CollectionViewCell,当滚动时会显示一个视图,类似于从烤面包机中跳出的吐司
📲 示例
为了运行示例项目,请克隆仓库,然后首先在 Example 目录中运行 pod install
📋 要求
ToastCollectionViewCell 需要 iOS 9 和 Swift 4
📦 安装
ToastCollectionViewCell 通过 CocoaPods 提供。要安装它,请简单地添加以下行到您的 Podfile 中
pod 'ToastCollectionViewCell', :git => 'https://github.com/frenchfalcon/ToastCollectionViewCell'
😏 用法
1. 创建一个 'Toast' 视图,您的单元格将在滚动时显示
创建一个从 UIView
类继承的类,具有您想要的形状和设计...
class MyToast: UIView {
// Your code, your design
}
2. 创建您的 UICollectionView,就像您通常会做的那样
-
实例化一个经典的
UICollectionView
-
实例化一个
ToastCollectionViewCellDelegate
delegate 重要: 它必须声明为一个类属性 -
这个
delegate
需要您的UICollectionView
的引用,传递视图! -
最后,将
UICollectionView
的delegate
属性设置为这个代理
可以决定吐司视图的偏移量
-
maxPositionForComponent
表示你希望在滚动时吐司能达到的最高位置 -
offsetToComponent
表示每个UICollectionView
与每个吐司之间的偏移量,用于触发移动
示例
class ViewController: UIViewController {
var toastDelegate: ToastCollectionViewDelegate() // Class property
var collectionView: UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
// Instanciate the UICollectionView
let collectionViewLayout = UICollectionViewLayout()
self.collectionView = UICollectionView(frame: CGRect, collectionViewLayout: collectionViewLayout)
// Set the offsets
self.toastDelegate.maxPositionForComponent = 75.0
self.toastDelegate.offsetToComponent = 20.0
// Pass the UICollectionView to the delegate
self.toastDelegate.collectionView = self.collectionView!
// Set the delegates
self.collectionView!.delegate = self.toastDelegate
self.collectionView!.dataSource = self // Implement your methods
}
}
3. 创建一个继承自ToastCollectionViewCell的UICollectionviewCell
重写toastViewForCell()
方法,并返回你的Toast视图
override func toastViewForCell() -> UIView? {
// Return the ToastView with the size you want
return MyToastView(frame: CGRect(x: 0, y: 0, width: 150, height: 50))
}
附加说明
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, [email protected]
🚔 许可证
ToastCollectionViewCell遵循MIT许可证。更多信息请参阅LICENSE文件。