قالب وردپرس درنا توس
Home / IOS Development / # 306: Customize Collection See Cell Insertion Animations 🚶🏼🏃🏼 – Little Bites of Cocoa

# 306: Customize Collection See Cell Insertion Animations 🚶🏼🏃🏼 – Little Bites of Cocoa



Subjects

Topics

UICollectionView has always been a power plant. Its diversity and versatility are hard to underestimate.

We've covered it a little here on LBOC but we still have just scratched the surface. Today, we begin to dive deeper into what all it can do by adapting how new collective cells animate when they are inserted . Let us begin!

We start by creating a new single display app in Xcode . [1

9659000] We want to extract a UICollectionViewController ] in Interface Builder set its class to be ViewController one that accompanies ] Xcode Template and then Update ViewController [ ] [] [] [] [ ] [ ] ] ()

FUNC addItem () { items . color . random ())] }

ride FUNC viewDidLoad () {
super [19659030]. viewDidLoad ()
to _ [19659052] in 0 ... 10 { addItem () ]
}

ride FUNC collectionView (
_ collectionView : UICollectionView
     numberOfItemsInSection Section : Int
) -> [19659015] Int {
Return Items . telle
}

ride FUNC collectionView (
_ collectionView : UICollectionView
     cellForItemAt indexPath : IndexPath
) -> UICollectionViewCell {
la cell = collectionView [19659027] dequeueReusableCell (
withReuseIdentifier . "ItemCell"
       to : indexPath
)

cell . [19659081] contentView
. background color = items [ indexPath . topic ] .
color

return cell
}
}

Then we will add it to a UINavigationController and add a UIBarButtonItem to add new items.

  We want  Control + Drag  the bar button in our  ViewController.swift  as a new  add:  action.        Sending :   UIBarButtonItem )    {19659018] {19659018] {19659018} 
    AddItem  () 

      indexPath    =   IndexPath  (
      element :.    even    items [19659030]   count    -    1  
     Section :    0 
  ) 

    CollectionView    . .?.           collectionView      insertItems  (  at :    [  indexPath  ] 
  },   completion :    nil ) 
} 

With this we have the best possible "boilerplate" layout. Only a few square, randomly colored cells fade in when they were inserted :

Not bad. Now let's customize the behavior of these cells as they are inserted . For this we need our own custom UICollectionViewLayout subclass.

In particular, we want the subclass UICollectionViewFlowLayout and override a few features to customize the behavior of [ : [IC65ollectionViewFlowLayout
var insertingIndexPaths = [ IndexPath [19659016]] ()
}

We have added a property to keep track of which index routes to fill our property, we must override a few override func prepare ( for ColectionViewUpdates updateItems : [ UICollectionViewUpdateItem ]) [19659018] {
super 19659028] prepare ( for ColectionViewUpdates : [19659026] updateItems ). insertingIndexPaths RemoveAll ()

to update in updateItems {
if la indexPath ] = update . indexPathAfterUpdate [19659016]
update . updateAction == . set {
insertingIndexPaths . føye indexPath )
}
}
}

ride FUNC finalizeCollectionViewUpdates () {[19659179] super . finalizeCollectionViewUpdates ()

insertingIndexPaths . RemoveAll ()
}

Fine. Nothing fancy here, we only collect prisoners index paths at the beginning of each update and remove them at the end.

The real magic happens when we override a last function:

  override    func    initialLayoutAttributesForAppearingItem  (
    at   itemIndexPath :   IndexPath [19659902]])    -?.>   UICollectionViewLayoutAttributes     {
    la   attributes    =    super    initialLayoutAttributesForAppearingItem  (  by .    itemIndexPath ) 

    if    insertingIndexPaths    contains  (  itemIndexPath  )    {
      attributes [19659016] .   a    =    0.0 
      attributes  .   transform    = [1,965,901 5] CGAffineTransform  (
        scaleX :    0.1  
         :    0.1 
    ) 
  } 

    Return    Attributes 
} 

Our Collection Show was to Go to to use recently to get layout attributes ] and modify them a bit before returning to the system.

Here we have added a transform to scale the item down when it is first added, this gives us a nice zoom up effect as each element is added:

It looks nice, but let's try something even more fun. We can change the conversion bar to:

  attributes ? .   ]     (
    TranslationX :    0 ]  
    y :    500.0 
) 

] With this one change we can achieve a completely different effect:

Tidy!

Download the Xcode project we built in this Bite here.

It's all for today. Do you have a specific UICollectionView question you want to answer? Send it together !.


Source link