UICollectionView照片库到全屏

时间:2013-09-24 20:13:51

标签: ios uicollectionview uicollectionviewlayout

我正在尝试使用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];
}

3 个答案:

答案 0 :(得分:1)

我的目标是: CollectionView A连续有3张照片网格。 当我点击其中一张照片时,它会使用水平滚动和全屏图像实现CollectionView B.

这里是代码:

1)CollectionView唯一的selectItem方法:

rep(list(c(2,6)),8)

此处- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { FullScreenViewController *fsvc = [FullScreenViewController new]; fsvc.ArrayForFullScr = self.Array; fsvc.currentIndex = [NSIndexPath indexPathForItem:indexPath.row inSection:1]; [self.navigationController pushViewController: fsvc animated: YES]; } - 是一个项目数组(照片)。

2)CollectionView B - self.Array

FullScreenViewController.h

@interface FullScreenViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> @property (nonatomic, strong) NSArray *arrayForFullScr; @property (nonatomic) NSIndexPath *currentIndex; @property (nonatomic, strong) UICollectionView *collectionView; @end

FullScreenViewController.m

我使用#import&#34; SDWebImage / UIImageView + WebCache.h&#34;存储图像。 您可以复制代码并对其进行自定义。

答案 1 :(得分:0)

答案 2 :(得分:0)

虽然@nik的回答是正确的。滚动到选定的indexpath时出现问题。它将无法正确滚动选定的索引路径。

如果您遇到问题,我建议您将scrollToItemAtIndexPath:方法放在viewWillAppear内。