Como rodar um UICollectionView semelhante ao aplicativo de fotos e manter a visão atual centralizada?
Eu tenho uma galeria de fotos que usa umUICollectionView
com umUICollectionViewFlowLayout
, tempagingEnabled
e rola horizontalmente mostrando apenas uma vista por vez.
Funciona muito bem até eu tentar rodá-lo ...
Quando eu girar o dispositivo,willRotateToInterfaceOrientation:duration:
Eu atualizo ocollectionView.contentOffset
por isso, fica no item correto e eu redimensiono o currentCell para que ele se anime nas novas dimensões.O problema está na animação entre os dois estados, a orientação 'anterior' animaa partir do canto superior esquerdo E arremessa na visão outras células. O que estou fazendo de errado, de modo que a vista sendo animada na tela seja FUBAR?
Aqui está o que parece em ação:
http://www.smugmug.com/gallery/n-3F9kD/i-BwzRzRf/A (ignore o vídeo agitado, isso é culpa do Quicktime: p)
Aqui está o meuwillAnimateRotationToInterfaceOrientation:duration
:
- (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;
}
Tanto quanto sei, não consigo encontrar nenhum código de exemplo em qualquer lugar que ilustre como fazer uma exibição de coleção de estilo 'galeria de fotos' que gira normalmente.