我想使用UICollectionView和UICollectionViewFlowLayout來製作照片庫。 這個想法是在垂直滾動的網格中顯示很多照片,當用戶點擊一張照片時,它會變成水平的,全屏的,帶有分頁。UICollectionView照片庫到全屏
我的問題是,當用戶在全屏模式下旋轉設備時,以及當他從網格切換到全屏時,動畫非常難看。
我嘗試過使用2種不同的集合佈局,並在它們之間切換,但問題仍然存在。
如果有人有這種行爲的示例應用程序或知道如何做,我會非常感激。
這就是我所擁有的。
@interface MovieImagesVC() <UICollectionViewDelegateFlowLayout>
@end
@implementation MovieImagesVC {
UICollectionViewFlowLayout *flowLayout;
int currentIndex;
}
- (void)viewDidLoad
{
[super viewDidLoad];
flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
collectionView.pagingEnabled = NO;
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
[flowLayout setMinimumLineSpacing:10.0f];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait ||
self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
flowLayout.itemSize = CGSizeMake(100.f, 100.f);
}
else {
flowLayout.itemSize = CGSizeMake(105.f, 100.f);
}
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
else {
collectionView.pagingEnabled = YES;
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
[flowLayout setMinimumLineSpacing:0.0f];
flowLayout.itemSize = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height-20);
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
[self.collectionView.collectionViewLayout invalidateLayout];
[collectionView reloadData];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}
#pragma mark Rotation
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self.collectionView.collectionViewLayout invalidateLayout];
if (flowLayout1.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
flowLayout1.itemSize = CGSizeMake(self.collectionView.frame.size.height, self.collectionView.frame.size.width-20);
}
else {
flowLayout1.itemSize = CGSizeMake(self.collectionView.frame.size.height, self.collectionView.frame.size.width-20);
}
}
else {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
flowLayout1.itemSize = CGSizeMake(100.f, 100.f);
}
else {
flowLayout1.itemSize = CGSizeMake(105.f, 100.f);
}
}
[self.collectionView reloadData];
CGPoint currentOffset = [self.collectionView contentOffset];
currentIndex = currentOffset.x/(int)self.collectionView.frame.size.width;
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}