As we all know Apple has announced new functionality in UICollectionview in iOS 10. These features are very interesting and useful. And I am very excited to explore my knowledge and new functionality with my iOS team.

Apple announced new thing in UICollectionView WWDC engineer conference. They shared exciting news that can change the iOS app development.

Apple also announced many other enhancements and inceptions at the conference. You can read the major updates here.

There are so many new things iOS 10 but here we are discussing new functionality in UICollectionview.

Let’s start…..

I mention some points below that announced by WWDC engineer in this conference

Smooth scrolling, self-sizing cells and Interactive reordering.

Smooth scrolling

Before going to discuss smooth scrolling we have to understand the concept of framing. When we scroll up the collection view we can see animation that performs at 60 frames per sec.So the user can use it in less than 16.67ms so this animation appears to a user very smooth.If frame rate drops lower than that it appears like choppy animation.

Below I share image of anatomy of dropped frame explaining by WWDC engineer

So if we want to show smooth scrolling we need to put some heavy loading method on the main thread.Now  after an understanding of drop frame, I am starting to explain how collection view lifecycle works in iOS 10.0 is as follows:

A collection view cell lifecycle is as follows:

  1. The cell is taken out of the reuse queue when it is about to come into view
  2. prepareForReuse is called to set to default state
  3. cellForItemAtIndexPath is called where we populate the cell – access data model, set it on the cell and return the cell
  4. willDisplayCell is called immediately before a cell is displayed
  5. didEndDisplayingCell is called when the cell goes out of view

Now you are thinking there is nothing new in this methods.These methods are same for iOS 9 and iOS 10 app development. But below image will clear it very well as explained at WWDC:

You can see above image. Always “willDisplayCell” method called before the cell display on the screen.So it will improve your CPU process speed and so your data will load before cell displays on the screen.

And Apple has added new feature “Prefetching”. Prefetching prepare your cell before cell will display on the screen.So this function used before cell is on the screen.

Prefetching is provided by the UICollectionViewDataSourcePrefetching protocol and contains two instance functions:

(1) Required:-

func collectionView(UICollectionView, prefetchItemsAt: [IndexPath])

(2)Optional:-

func collectionView(UICollectionView, cancelPrefetchingForItemsAt: [IndexPath])

You can use this method like:

func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {

  for indexPath in indexPaths {

      // calculate/update/collect some data

      cellArray[indexPath.row] = updatedData

  }

}

As mentioned above, the cancelPrefetchingForItemsAt function is an optional feature that allows you to revert or clean up your data source when prefetching for an array of cells has been canceled by the collection view.

If you want to disable collection view prefetching make UICollectionView isPrefetchingEnabled property to false.

As an aside, Apple has also provided prefetching for the UITableView classes with the new UITableViewDataSourcePrefetching protocol.

self-sizing:-

Apple said that there are 3 methods for collection view cell size from the screenshot below:

Realizing this, Apple has introduced automatic item sizing for your UICollectionViewFlowLayout. Simple set the item size to the constant UICollectionViewFlowLayoutAutomaticSize and UIKit will make past measurements of your items in order to automatically set item sizes for future items.

Interactive Reordering:-

iOS 10 UICollectionView has also improved .
Prior to iOS 10,For supporting reordering need to call following API of collection view:

1.beginInteractiveMovementForItem method call if collection view hit based on gesture.this method  returns NO if reordering was prevented from beginning otherwise YES

open func beginInteractiveMovementForItem(at indexPath: IndexPath) -> Bool

2.updateInteractiveMovementTargetPosition  method call for update target Position

open func updateInteractiveMovementTargetPosition(_ targetPosition: CGPoint)

3.endInteractiveMovement method call for confirm target position and want to end interaction movement.

open func endInteractiveMovement()

 

4.cancelInteractiveMovement method call for cancel reodering.

open func cancelInteractiveMovement()

After calling all methods just implement DataSource for Collection view and set installsStandardGestureForInteractiveMovement  Property to True.

collectionView.installsStandardGestureForInteractiveMovement = true

In iOS 10 no new api but now API support Pagination .for this simply enable pagination of collection view.

collectionView.isPagingEnabled = true

Along with iOS 10 if target cell move to the edge of the screen it automatically scroll in pagesize increment.Here is an example of paging is true.

And the end of an announcement Apple engineer said something awesome and it was, pull to refresh will be supported on:

– UIScrollView

– UITableView

– UICollectionView.

Example:-

If you followed the WWDC conference and have something to share on the same, do write us at info@letsnurture.com.

 

 

Thanks for reading.

 

Have something to share on the same? Tweet us on Twitter @letsnurture or drop a post on the official FaceBook page of LetsNurture. Share your thoughts with us, we will be happy to have a discussion on the same.

Want to work with us? We're hiring!