title: Advanced UICollectionView animation using invalidateLayout calls
body: <p>The <a href="https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CollectionViewBasics/CollectionViewBasics.html#//apple_ref/doc/uid/TP40012334-CH2-SW5" rel="noreferrer">UICollectionView Programming Guide</a> says:</p>
  
  <blockquote>
    <p>In addition to animating insertions, deletions, and move operations,
    you can invalidate the layout at any time and force it to redraw its
    contents. Invalidating the layout does not animate items directly;
    when you invalidate the layout, the collection view displays the items
    in their newly calculated positions without animating. However, the
    act of invalidating the layout causes the layout object to move items
    explicitly. In a custom layout, you might use this behavior to
    position cells at regular intervals and create an animated effect.</p>
  </blockquote>
  
  <p>I've been trying something like this, where <code>AnimationStep</code> is an enum used by my <code>UICollectionViewFlowLayout</code> subclass to conditionally set the positions of tiles with a three stage animation:</p>
  
  <pre><code>-(void)update{
      [self animateLayoutAfterDelay:2.0 toStep:AnimationStepOne];
      [self animateLayoutAfterDelay:4.0 toStep:AnimationStepTwo];
      [self animateLayoutAfterDelay:6.0 toStep:AnimationStepThree];
  }
  
  -(void)animateLayoutAfterDelay:(NSTimeInterval)delay toStep:(MagazineLayoutAnimationStep)step {
      double delayInSeconds = delay;
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
          [UIView animateWithDuration:2.0 animations:^{
              self.layout.animationStep = step;
              [self.layout invalidateLayout];
          }];
      });
  }
  </code></pre>
  
  <p>This has quite unpredictable effects. Some cells animate in the way I'd expect, some get hidden and shown or just appear in their new locations. I put random background colours on the cells to see if this might be the effect of UICollectionView recycling cells and sure enough, sometimes it was. Which explains some of the weirdness, but not all of it.</p>
  
  <p>Does anybody know how Apple want me to animate cells and move them around?</p>
  
  <p>(I need this because my collection view cells tend to change size and I want a graceful animation without any diagonal movement).</p>