我正在尝试使用 UICollectionView 和 UICollectionViewFlowLayout 制作照片库。这个想法是在一个垂直滚动的网格中显示许多照片,当用户点击一张时,它会变成水平的、全屏的、分页的。
我的问题是当用户在全屏模式下旋转设备时,当他从网格切换到全屏时,动画非常难看。
我尝试使用 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];
}