قالب وردپرس درنا توس
Home / IOS Development / # 307: Animated collection See setup changes 📱🚶🏼🏃🏼 – Small pieces of cocoa

# 307: Animated collection See setup changes 📱🚶🏼🏃🏼 – Small pieces of cocoa



Issue

Topics

Today we continue to look at UICollectionView by dive into animation . Let us begin.

We start with the same simple baseline collection view check we created at the beginning of Bite # 306.

Now we add two new collection viewer layouts to our display regulator ] : UICollectionViewFlowLayout = {
la layout = UICollectionViewFlowLayout () layout . itemSize = CGSize ( width : 75 heights : 75 )

return layout
} ()

Var large . UICollectionViewFlowLayout = {
la layout = UICollectionViewFlowLayout ()

layout [1

9659022] itemSize = ] : 150 height : 150 )

return layout
} ( )

Then we override init on our display regulator and give it our "small" layout:

  init  ()    {
    super .   init  (  collectionViewLayout :    small ) 
} 

Fine. At this time, we have a basic collection control rolling our small cells :

Now we add code to swap between our two layouts when the user loses some cell:

  override    func    collectionView  (  _    collectionView :    UICollectionView     didSelectItemAt    indexPath .    IndexPath )    {
    la    newLayout    =    (  collectionView  [19659022] collectionViewLayout    == [19659034] small       large  :?.    small ) 

    collectionView    setCollectionViewLayout  (  newLayout     animated :    true ) 
} 

Now we can switch back and forth between v make two layouts with a nice look. Very cool.

It's superhuman how the function setCollectionViewLayout is smart enough to change the animation so that the cell we chose is still in sight

  where    ] Large [19659000]]    Large .    UICollectionViewFlowLayout    =    {
    la    layout    =    UICollectionViewFlowLayout  () 

    layout    itemSize [19659013] =    ] CGSize  [  width :    300     height :    300 ) 

    return    Layout 
} ( ) 

Nice!

Download the Xcode project we built in this bit here.

A final note here: This is just an approach. There are tons of different ways to achieve this (and similar) behavior. We'll take a closer look when we go deeper into collections in the forthcoming Bites .

Have a specific UICollectionView question you want to answer? Send it together !.


Source link