title: How to rotate a UICollectionView similar to the photos app and keep the current view centered?
body: <p>I have a photo gallery view that uses a <code>UICollectionView</code> with a <code>UICollectionViewFlowLayout</code>, it has <code>pagingEnabled</code> and scrolls horizontally showing only one view at a time.</p>
  
  <p>Works great till I try to rotate it...</p>
  
  <p>When I rotate the device, in <code>willRotateToInterfaceOrientation:duration:</code> I update the <code>collectionView.contentOffset</code> so it stays on the correct item and I resize the currentCell so it animates into the new dimensions.  <strong>The problem is in the animation between the two states, the 'previous' orientation animates <em>from the upper left corner</em> AND flings into the view other cells.</strong>  What is it I'm doing wrong such that the view being animated off the screen is FUBAR?</p>
  
  <p>Here is what it looks like in action:</p>
  
  <p><a href="http://www.smugmug.com/gallery/n-3F9kD/i-BwzRzRf/A" rel="noreferrer">http://www.smugmug.com/gallery/n-3F9kD/i-BwzRzRf/A</a> (ignore the choppy video, thats Quicktime's fault :p)</p>
  
  <p>Here is my <code>willAnimateRotationToInterfaceOrientation:duration</code>:</p>
  
  <pre><code>- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
  
  // Update the flowLayout's size to the new orientation's size
  UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
  if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
      flow.itemSize = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
  } else {
      flow.itemSize = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
  }
  self.collectionView.collectionViewLayout = flow;
  [self.collectionView.collectionViewLayout invalidateLayout];
  
  // Get the currently visible cell
  PreviewCellView *currentCell = (PreviewCellView*)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:_currentIndex inSection:0]];
  
  // Resize the currently index to the new flow's itemSize
  CGRect frame = currentCell.frame;
  frame.size = flow.itemSize;
  currentCell.frame = frame;
  
  // Keep the collection view centered by updating the content offset
  CGPoint newContentOffset = CGPointMake(_currentIndex * frame.size.width, 0);
  self.collectionView.contentOffset = newContentOffset;
  }
  </code></pre>
  
  <p>As far as I'm aware I can't find any sample code anywhere that illustrates how to make a 'photo gallery' style collection view that rotates gracefully.</p>