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...
classMyToastView:UIView{
// Your code here
}Instantiate a
ToastCollectionViewImplement the
datasourceanddelegate
maxPositionForComponentis how high you want your toast to go when scrollingoffsetToComponentis the offset between theCollectionViewand each toast for triggering movements
import ToastCollectionView
classViewController:UIViewController{varcollectionView:ToastCollectionView?overridefunc viewDidLoad(){
super.viewDidLoad()
// Instanciate the CollectionView
letcollectionViewLayout=UICollectionViewLayout()self.collectionView =ToastCollectionView(frame: CGRect, collectionViewLayout: collectionViewLayout)
// Set the offsets
self.collectionView.maxPositionForComponent =75.0self.collectionView.offsetToComponent =30.0
// Set the delegates
self.collectionView.delegate =selfself.collectionView.dataSource =self}}Override the toastViewForCell() method, and return your Toast view
overridefunc toastViewForCell()->UIView?{
// Return the Toast view with the size you want
returnMyToastView(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
overridefunc widthForExpandedToastView()->CGFloat?{return190.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.
classExampleCell:ToastCollectionViewCell,ToastCollectionViewCellDelegate{func onToastFullyRaised(toast:UIView){lettoast= toast as!MyToastView
toast.doSomething()}}Oscar Gotting (@Scaraux)
ToastCollectionView is available under the MIT license. See the LICENSE file for more info.
