In the Apple's own documentation, it states the steps on how to build that behavior, but I don't quite understand, can somebody tell me how to do that in code.
To animate collection views, create an animator with the init(collectionViewLayout:) method. The resulting animator employs a collection view layout (an object of the UICollectionViewLayout class) for its coordinate system. The dynamic items in this sort of animator must be UICollectionViewLayoutAttributes objects that are part of the layout. You can define a boundary, for items participating in a collision behavior, relative to the bounds of the collection view layout. See the setTranslatesReferenceBoundsIntoBoundary(with:) method. A collection view animator automatically calls the invalidateLayout() method as needed, and automatically pauses and resumes animation, as appropriate, when you change a collection view’s layout.
This is a little out-of-date, but looks like an interesting introduction to it: https://www.objc.io/issues/5-ios7/collection-views-and-uidynamics/