ToastCollectionView is a Swift UICollectionView
that pushes and displays views when scrolling, reminding of a toast popping out of a toaster π
It is useful is you wish to display information about the content of your cells
To run the example project, clone the repo, and run pod install
from the Example directory first.
ToastCollectionView requires iOS 9 and Swift 4
ToastCollectionView is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ToastCollectionView'
Create a class that inherits from the UIView
class, with the shape and design you want...
class MyToastView: UIView {
// Your code here
}
-
Instantiate a
ToastCollectionView
-
Implement the
datasource
anddelegate
-
maxPositionForComponent
is how high you want your toast to go when scrolling -
offsetToComponent
is the offset between theCollectionView
and each toast for triggering movements
import ToastCollectionView
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
}
}
Override the toastViewForCell()
method, and return your Toast view
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))
}
If you wish to expand the toast view when it has reach the maximum vertical position, override the widthForExpandedToastView()
method and return the new width
override func widthForExpandedToastView() -> CGFloat? {
return 190.0
}
The ToastCollectionViewCell
offers a preRaiseToastView(toPosition: CGFloat)
method that allows you to force some Toast views to be risen. It's useful for example for the first cells that appears when you present the CollectionView, so they don't pop only when the user starts to scroll.
By implementing the ToastCollectionViewCellDelegate
, you can be notified when any of your Toast views get to
their top position, meaning they are fully raised.
You can then trigger an action on the given Toast view.
class ExampleCell: ToastCollectionViewCell, ToastCollectionViewCellDelegate {
func onToastFullyRaised(toast: UIView) {
let toast = toast as! MyToastView
toast.doSomething()
}
}
Oscar Gotting (@Scaraux)
ToastCollectionView is available under the MIT license. See the LICENSE file for more info.