際際滷

際際滷Share a Scribd company logo
Introducing Collection Views
Mark Pospesel
Mobile Architect
Y Media Labs
code	 github.com/mpospese/IntroducingCollectionViews
slides	 bit.ly/ViTNiK
@mpospese
際際滷s and code on
?ash drives
Who Am I?
? Studied mathematics, marine biology, and teaching English as a Second
Language
? 14 years industry experience
? Programming for mobile since 1999!
? Mobile Architect for Y Media Labs
Outline
? Introduction
? API
? Flow Layout
? Custom Layouts
? More Customizations
? Summary
Introduction
Where we came from
? Table views
What we also wanted
Grid views and horizontal scrollers
The hard way
? UIScrollView
? manually place ^cells ̄
? DataSource delegate
? delegate for selection, appearance, disappearance, etc.
? create views for cells on demand and cache cells for reuse
What if you wanted something even more complex?
Enter UICollectionView
? Handles grids
? Handles horizontal line scrollers
? Handles pretty much any custom layout you can imagine
? High-performance even with large data sets
? Works well with Core Data and NSFetchedResultsController
? Familiar delegate / data source API pattern
? Disclaimer: Never ship
programmer art!
Components
? Cells
Components
? Cells
Components
? Cells
? UICollectionViewCell
? UIView
? UIImageView
? UILabel
Components
? Cells
? Supplementary Views (headers)
Components
? Cells
? Supplementary Views (headers
and footers)
Components
? Cells
? Supplementary Views (headers
and footers)
? Decoration Views (everything
else)
Components
API
UICollectionView
? If you know how to use UITableView, you mostly know how to use simple
Collection Views already
UICollectionViewDataSource
? number of sections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
? number of items in each section
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:
(NSInteger)section
? con?gure cells
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
? con?gure supplementary views
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
Cell and view reuse
? Cells, supplementary views, and decoration views are all reused
? Each type of view needs to be registered for reuse (nib or class)
registerClass:forCellWithReuseIdentifier:
registerNib:forCellWithReuseIdentifier:
registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
Cell and view reuse
? Cells, supplementary views, and decoration views are all reused
? Each type of view needs to be registered for reuse (nib or class)
? dequeue will instantiate the class if necessary
dequeueReusableCellWithReuseIdentifier:forIndexPath:
dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
UICollectionViewDelegate
? Manages selecting and highlighting
? should/did select/deselect
? should/did highlight/unhighlight
? Tracks the removal of cells
? didEndDisplay cell/supplementary view
? Manages actions for cells
? should show menu
? canPerform/perform action on cell
Layouts
UICollectionViewLayout
? Tells the collection view about position, size, and visual state of items in
the collection
? abstract class
UICollectionViewFlowLayout
? Subclass of UICollectionViewLayout for grids
? Makes it super easy to implement basic grids
? Supports headers and footers
? Can be customized via properties or by delegate
? property = global
? delegate = per item or section
? Can be subclassed
UICollectionViewFlowLayout
? scroll direction
UICollectionViewFlowLayout
? scroll direction
UICollectionViewFlowLayout
? scroll direction
? itemSize
UICollectionViewFlowLayout
? scroll direction
? itemSize
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimum spacing
? actual spacing
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
? headerReferenceSize
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
? headerReferenceSize
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
? headerReferenceSize
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
? headerReferenceSize
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
? headerReferenceSize
? footerReferenceSize
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
? headerReferenceSize
? footerReferenceSize
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
? headerReferenceSize
? footerReferenceSize
UICollectionViewFlowLayout
? scroll direction
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
? headerReferenceSize
? footerReferenceSize
UICollectionViewDelegateFlowLayout
? extends UICollectionViewDelegate
? customize per section (size per item)
UICollectionViewFlowLayout
? itemSize
? minimumInteritemSpacing
? minimumLineSpacing
? sectionInset
? referenceSizeForHeader
? referenceSizeForFooter
UICollectionViewDelegateFlowLayout
? C?collectionView:layout:sizeForItemAtIndexPath:
? C?collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
? C?collectionView:layout:minimumLineSpacingForSectionAtIndex:
? C?collectionView:layout:insetForSectionAtIndex:
? C?collectionView:layout:referenceSizeForHeaderInSection:
? C?collectionView:layout:referenceSizeForFooterInSection:
DEMO
Flow Layouts
Custom Layouts
UICollectionViewLayoutAttributes
? Describe position, size, visual state of collection items
? Apply to cells, supplementary views, and decoration views
? Set by layout
? Used by collection view to con?gure cells and views
? Subclass to add additional attributes
UICollectionViewLayoutAttributes
? position
? size
? opacity
? zIndex
? transform
When to subclass UICollectionViewFlowLayout
? add decoration views
? add extra supplementary views (beyond headers and footers)
? adjust the default layout attributes of items
? add new layout attributes
? use custom insert or delete animations
When to subclass UICollectionViewLayout
? The layout you want is nothing like a grid
? You¨d have to change the default attributes so much that it would be less
work to not derive from ?ow layout
How UICollectionViewLayout works
? prepareLayout
? collectionViewContentSize
? layoutAttributesForElementsInRect:
? provide layout attributes on demand
layoutAttributesForItemAtIndexPath:
layoutAttributesForSupplementaryViewOfKind:atIndexPath:
layoutAttributesForDecorationViewOfKind:atIndexPath:
? invalidateLayout
Invalidating layouts
? explicit: invalidateLayout
? implicit: performBatchUpdates:completion:
? implicit: override shouldInvalidateLayoutForBoundsChange:
? called when contentO?set changes or when bounds change
? always return YES
? return YES only if bounds change
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)oldBounds
Miscellaneous Tips
? don¨t use collectionView.frame / bounds outside of prepareLayout
? consider setting hidden = YES instead of alpha = 0
DEMO
Custom Layouts
More Customizations
More Customizations
? Decoration Views
? Custom Layout Attributes
? Custom Insert & Delete Animations
How to add decoration views
? Create your own layout class
? Register your decoration view
registerNib:forDecorationViewOfKind:
registerClass:forDecorationViewOfKind:
? Return attributes for all decoration views
layoutAttributesForElementsInRect:
layoutAttributesForDecorationViewOfKind:atIndexPath:
When to subclass UICollectionViewLayoutAttributes
? You need additional attributes beyond those found in the base class
How to create custom attributes
? Subclass UICollectionViewLayoutAttributes
? add additional properties
? Important! You must implement copyWithZone:
- (id)copyWithZone:(NSZone *)zone
{
CustomAttributes *attributes = [super copyWithZone:zone];
attributes.customProperty = self.customProperty;
return attributes;
}
How to create custom attributes
? Subclass UICollectionViewLayoutAttributes
? Subclass UICollectionViewLayout (or ?ow layout)
? implement layoutAttributesClass to return your custom class
+ (Class)layoutAttributesClass
{
return [CustomAttributes class];
}
How to create custom attributes
? Subclass UICollectionViewLayoutAttributes
? Subclass UICollectionViewLayout (or ?ow layout)
? implement layoutAttributesClass to return your custom class
? set your custom attributes
layoutAttributesForElementsInRect:
layoutAttributesForItemAtIndexPath:
layoutAttributesForSupplementaryViewOfKind:atIndexPath:
layoutAttributesForDecorationViewOfKind:atIndexPath:
How to create custom attributes
? Subclass UICollectionViewLayoutAttributes
? Subclass UICollectionViewLayout (or ?ow layout)
? In your cell, supplementary view, or decoration view classes implement
applyLayoutAttributes:
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
if ([layoutAttributes isKindOfClass:[CustomAttributes class]])
{
CustomAttributes *customAttributes = (CustomAttributes *)layoutAttributes;
// Do something interesting with them
}
}
DEMO
Custom layout attributes
UICollectionView animations
? switch layout
? insert items
? delete items
? move items
- (void)setCollectionViewLayout:animated:
- (void)insertItemsAtIndexPaths:
- (void)deleteItemsAtIndexPaths:
- (void)moveItemAtIndexPath:toIndexPath:
Delete animations	
? Deleted item fades away
? Remaining items move to their new positions
? override ?nalLayoutAttributesForDisappearingItemAtIndexPath:
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:
(NSIndexPath *)itemIndexPath
{
UICollectionViewLayoutAttributes* attributes = [self
layoutAttributesForItemAtIndexPath:itemIndexPath];
// Configure attributes ...
return attributes;
}
Customizing delete animations
? override ?nalLayoutAttributesForDisappearingItemAtIndexPath:
? gets called for every item that needs to be moved too, so need to track
index of deleted item(s).
? do that in prepareForCollectionViewUpdates: (don¨t forget to call super!)
Customizing delete animations
What they didn¨t tell you
- (void)prepareForCollectionViewUpdates:(NSArray *)updateItems
{
[super prepareForCollectionViewUpdates:updateItems];
self.deleteIndexPaths = [NSMutableArray array];
for (UICollectionViewUpdateItem *update in updateItems)
{
if (update.updateAction == UICollectionUpdateActionDelete)
{
[self.deleteIndexPaths addObject:update.indexPathBeforeUpdate];
}
}
}
? override ?nalLayoutAttributesForDisappearingItemAtIndexPath:
? gets called for every item that needs to be moved too, so need to track
index of deleted item(s).
? do that in prepareForCollectionViewUpdates: (don¨t forget to call super!)
? clean up in ?nalizeCollectionViewUpdates
Customizing delete animations
What they didn¨t tell you
- (void)finalizeCollectionViewUpdates
{
[super finalizeCollectionViewUpdates];
self.deleteIndexPaths = nil;
}
? override ?nalLayoutAttributesForDisappearingItemAtIndexPath:
Customizing delete animations
What they didn¨t tell you
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:
(NSIndexPath *)itemIndexPath
{
if ([self.deleteIndexPaths containsObject:itemIndexPath])
{
UICollectionViewLayoutAttributes* attributes = [self
layoutAttributesForItemAtIndexPath:itemIndexPath];
// Configure attributes ...
return attributes;
}
return nil;
}
? same applies to initialLayoutAttributesForAppearingItemAtIndexPath:
? will get called for both insert and delete animations
? must call super
Customizing delete animations
But wait, there¨s more!
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:
(NSIndexPath *)itemIndexPath
{
UICollectionViewLayoutAttributes *attributes = [super
initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
if ([self.insertIndexPaths containsObject:itemIndexPath])
{
if (!attributes)
attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
// Configure attributes ...
}
return attributes;
}
DEMO
Custom delete animation
Summary
Summary
? Collection views are very powerful, ?exible tool
? UICollectionViewFlowLayout is highly customizable via properties,
subclassing, or delegation
? UICollectionView can support any arbitrary layout
? If you can dream it, build a layout for it!
Support for iOS 4.3 & 5
PSTCollectionView
? GitHub project by Peter Steinberger
? uses UICollectionView classes under iOS 6
? uses custom classes under iOS 4.3 and 5
? doesn¨t have all the animations (yet)
? github.com/steipete/PSTCollectionView
Resources
? github.com/mpospese/IntroducingCollectionViews
? github.com/steipete/PSTCollectionView
? developer.apple.com/library/ios/documentation/WindowsViews/
Conceptual/CollectionViewPGforIOS/CollectionViewPGforIOS.pdf
? WWDC 2012, Session 205 (Introducing Collection Views)
? WWDC 2012, Session 219 (Advanced Collection Views and Building
Custom Layouts)
Contact
twitter! @mpospese
email! mark.pospesel@ymedialabs.com
phone! 650.260.5227
web!! ymedialabs.com
blog!! markpospesel.com

More Related Content

Introducing collection views - Mark Pospesel

  • 1. Introducing Collection Views Mark Pospesel Mobile Architect Y Media Labs code github.com/mpospese/IntroducingCollectionViews slides bit.ly/ViTNiK @mpospese 際際滷s and code on ?ash drives
  • 2. Who Am I? ? Studied mathematics, marine biology, and teaching English as a Second Language ? 14 years industry experience ? Programming for mobile since 1999! ? Mobile Architect for Y Media Labs
  • 3. Outline ? Introduction ? API ? Flow Layout ? Custom Layouts ? More Customizations ? Summary
  • 5. Where we came from ? Table views
  • 6. What we also wanted
  • 7. Grid views and horizontal scrollers The hard way ? UIScrollView ? manually place ^cells ̄ ? DataSource delegate ? delegate for selection, appearance, disappearance, etc. ? create views for cells on demand and cache cells for reuse
  • 8. What if you wanted something even more complex?
  • 9. Enter UICollectionView ? Handles grids ? Handles horizontal line scrollers ? Handles pretty much any custom layout you can imagine ? High-performance even with large data sets ? Works well with Core Data and NSFetchedResultsController ? Familiar delegate / data source API pattern
  • 10. ? Disclaimer: Never ship programmer art! Components
  • 13. ? Cells ? UICollectionViewCell ? UIView ? UIImageView ? UILabel Components
  • 14. ? Cells ? Supplementary Views (headers) Components
  • 15. ? Cells ? Supplementary Views (headers and footers) Components
  • 16. ? Cells ? Supplementary Views (headers and footers) ? Decoration Views (everything else) Components
  • 17. API
  • 18. UICollectionView ? If you know how to use UITableView, you mostly know how to use simple Collection Views already
  • 19. UICollectionViewDataSource ? number of sections - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView ? number of items in each section - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section ? con?gure cells - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath ? con?gure supplementary views - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  • 20. Cell and view reuse ? Cells, supplementary views, and decoration views are all reused ? Each type of view needs to be registered for reuse (nib or class) registerClass:forCellWithReuseIdentifier: registerNib:forCellWithReuseIdentifier: registerClass:forSupplementaryViewOfKind:withReuseIdentifier: registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
  • 21. Cell and view reuse ? Cells, supplementary views, and decoration views are all reused ? Each type of view needs to be registered for reuse (nib or class) ? dequeue will instantiate the class if necessary dequeueReusableCellWithReuseIdentifier:forIndexPath: dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
  • 22. UICollectionViewDelegate ? Manages selecting and highlighting ? should/did select/deselect ? should/did highlight/unhighlight ? Tracks the removal of cells ? didEndDisplay cell/supplementary view ? Manages actions for cells ? should show menu ? canPerform/perform action on cell
  • 24. UICollectionViewLayout ? Tells the collection view about position, size, and visual state of items in the collection ? abstract class
  • 25. UICollectionViewFlowLayout ? Subclass of UICollectionViewLayout for grids ? Makes it super easy to implement basic grids ? Supports headers and footers ? Can be customized via properties or by delegate ? property = global ? delegate = per item or section ? Can be subclassed
  • 30. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing
  • 31. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimum spacing ? actual spacing
  • 32. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing
  • 33. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing
  • 34. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset
  • 35. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset
  • 36. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset ? headerReferenceSize
  • 37. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset ? headerReferenceSize
  • 38. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset ? headerReferenceSize
  • 39. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset ? headerReferenceSize
  • 40. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset ? headerReferenceSize ? footerReferenceSize
  • 41. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset ? headerReferenceSize ? footerReferenceSize
  • 42. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset ? headerReferenceSize ? footerReferenceSize
  • 43. UICollectionViewFlowLayout ? scroll direction ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset ? headerReferenceSize ? footerReferenceSize
  • 45. UICollectionViewFlowLayout ? itemSize ? minimumInteritemSpacing ? minimumLineSpacing ? sectionInset ? referenceSizeForHeader ? referenceSizeForFooter
  • 46. UICollectionViewDelegateFlowLayout ? C?collectionView:layout:sizeForItemAtIndexPath: ? C?collectionView:layout:minimumInteritemSpacingForSectionAtIndex: ? C?collectionView:layout:minimumLineSpacingForSectionAtIndex: ? C?collectionView:layout:insetForSectionAtIndex: ? C?collectionView:layout:referenceSizeForHeaderInSection: ? C?collectionView:layout:referenceSizeForFooterInSection:
  • 49. UICollectionViewLayoutAttributes ? Describe position, size, visual state of collection items ? Apply to cells, supplementary views, and decoration views ? Set by layout ? Used by collection view to con?gure cells and views ? Subclass to add additional attributes
  • 51. When to subclass UICollectionViewFlowLayout ? add decoration views ? add extra supplementary views (beyond headers and footers) ? adjust the default layout attributes of items ? add new layout attributes ? use custom insert or delete animations
  • 52. When to subclass UICollectionViewLayout ? The layout you want is nothing like a grid ? You¨d have to change the default attributes so much that it would be less work to not derive from ?ow layout
  • 53. How UICollectionViewLayout works ? prepareLayout ? collectionViewContentSize ? layoutAttributesForElementsInRect: ? provide layout attributes on demand layoutAttributesForItemAtIndexPath: layoutAttributesForSupplementaryViewOfKind:atIndexPath: layoutAttributesForDecorationViewOfKind:atIndexPath: ? invalidateLayout
  • 54. Invalidating layouts ? explicit: invalidateLayout ? implicit: performBatchUpdates:completion: ? implicit: override shouldInvalidateLayoutForBoundsChange: ? called when contentO?set changes or when bounds change ? always return YES ? return YES only if bounds change - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)oldBounds
  • 55. Miscellaneous Tips ? don¨t use collectionView.frame / bounds outside of prepareLayout ? consider setting hidden = YES instead of alpha = 0
  • 58. More Customizations ? Decoration Views ? Custom Layout Attributes ? Custom Insert & Delete Animations
  • 59. How to add decoration views ? Create your own layout class ? Register your decoration view registerNib:forDecorationViewOfKind: registerClass:forDecorationViewOfKind: ? Return attributes for all decoration views layoutAttributesForElementsInRect: layoutAttributesForDecorationViewOfKind:atIndexPath:
  • 60. When to subclass UICollectionViewLayoutAttributes ? You need additional attributes beyond those found in the base class
  • 61. How to create custom attributes ? Subclass UICollectionViewLayoutAttributes ? add additional properties ? Important! You must implement copyWithZone: - (id)copyWithZone:(NSZone *)zone { CustomAttributes *attributes = [super copyWithZone:zone]; attributes.customProperty = self.customProperty; return attributes; }
  • 62. How to create custom attributes ? Subclass UICollectionViewLayoutAttributes ? Subclass UICollectionViewLayout (or ?ow layout) ? implement layoutAttributesClass to return your custom class + (Class)layoutAttributesClass { return [CustomAttributes class]; }
  • 63. How to create custom attributes ? Subclass UICollectionViewLayoutAttributes ? Subclass UICollectionViewLayout (or ?ow layout) ? implement layoutAttributesClass to return your custom class ? set your custom attributes layoutAttributesForElementsInRect: layoutAttributesForItemAtIndexPath: layoutAttributesForSupplementaryViewOfKind:atIndexPath: layoutAttributesForDecorationViewOfKind:atIndexPath:
  • 64. How to create custom attributes ? Subclass UICollectionViewLayoutAttributes ? Subclass UICollectionViewLayout (or ?ow layout) ? In your cell, supplementary view, or decoration view classes implement applyLayoutAttributes: - (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes { if ([layoutAttributes isKindOfClass:[CustomAttributes class]]) { CustomAttributes *customAttributes = (CustomAttributes *)layoutAttributes; // Do something interesting with them } }
  • 66. UICollectionView animations ? switch layout ? insert items ? delete items ? move items - (void)setCollectionViewLayout:animated: - (void)insertItemsAtIndexPaths: - (void)deleteItemsAtIndexPaths: - (void)moveItemAtIndexPath:toIndexPath:
  • 67. Delete animations ? Deleted item fades away ? Remaining items move to their new positions
  • 68. ? override ?nalLayoutAttributesForDisappearingItemAtIndexPath: - (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath: (NSIndexPath *)itemIndexPath { UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath]; // Configure attributes ... return attributes; } Customizing delete animations
  • 69. ? override ?nalLayoutAttributesForDisappearingItemAtIndexPath: ? gets called for every item that needs to be moved too, so need to track index of deleted item(s). ? do that in prepareForCollectionViewUpdates: (don¨t forget to call super!) Customizing delete animations What they didn¨t tell you - (void)prepareForCollectionViewUpdates:(NSArray *)updateItems { [super prepareForCollectionViewUpdates:updateItems]; self.deleteIndexPaths = [NSMutableArray array]; for (UICollectionViewUpdateItem *update in updateItems) { if (update.updateAction == UICollectionUpdateActionDelete) { [self.deleteIndexPaths addObject:update.indexPathBeforeUpdate]; } } }
  • 70. ? override ?nalLayoutAttributesForDisappearingItemAtIndexPath: ? gets called for every item that needs to be moved too, so need to track index of deleted item(s). ? do that in prepareForCollectionViewUpdates: (don¨t forget to call super!) ? clean up in ?nalizeCollectionViewUpdates Customizing delete animations What they didn¨t tell you - (void)finalizeCollectionViewUpdates { [super finalizeCollectionViewUpdates]; self.deleteIndexPaths = nil; }
  • 71. ? override ?nalLayoutAttributesForDisappearingItemAtIndexPath: Customizing delete animations What they didn¨t tell you - (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath: (NSIndexPath *)itemIndexPath { if ([self.deleteIndexPaths containsObject:itemIndexPath]) { UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath]; // Configure attributes ... return attributes; } return nil; }
  • 72. ? same applies to initialLayoutAttributesForAppearingItemAtIndexPath: ? will get called for both insert and delete animations ? must call super Customizing delete animations But wait, there¨s more! - (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath: (NSIndexPath *)itemIndexPath { UICollectionViewLayoutAttributes *attributes = [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath]; if ([self.insertIndexPaths containsObject:itemIndexPath]) { if (!attributes) attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath]; // Configure attributes ... } return attributes; }
  • 75. Summary ? Collection views are very powerful, ?exible tool ? UICollectionViewFlowLayout is highly customizable via properties, subclassing, or delegation ? UICollectionView can support any arbitrary layout ? If you can dream it, build a layout for it!
  • 76. Support for iOS 4.3 & 5 PSTCollectionView ? GitHub project by Peter Steinberger ? uses UICollectionView classes under iOS 6 ? uses custom classes under iOS 4.3 and 5 ? doesn¨t have all the animations (yet) ? github.com/steipete/PSTCollectionView
  • 77. Resources ? github.com/mpospese/IntroducingCollectionViews ? github.com/steipete/PSTCollectionView ? developer.apple.com/library/ios/documentation/WindowsViews/ Conceptual/CollectionViewPGforIOS/CollectionViewPGforIOS.pdf ? WWDC 2012, Session 205 (Introducing Collection Views) ? WWDC 2012, Session 219 (Advanced Collection Views and Building Custom Layouts)
  • 78. Contact twitter! @mpospese email! mark.pospesel@ymedialabs.com phone! 650.260.5227 web!! ymedialabs.com blog!! markpospesel.com