[英]UICollectionView photo gallery to fullscreen
I'm trying to make a Photo Gallery using UICollectionView and UICollectionViewFlowLayout. 我正在尝试使用UICollectionView和UICollectionViewFlowLayout创建一个图片库。 The idea is to show many photos in a grid with vertical scrolling, and when the user taps one it becomes horizontal, fullscreen, with paging. 这个想法是通过垂直滚动将许多照片显示在一个网格中,当用户点击其中的一张照片时,它将变成水平,全屏和分页显示。
My problem is when the user rotates the device while it's on fullscreen mode, and when he switches from grid to fullscreen, the animations are very ugly. 我的问题是,当用户在全屏模式下旋转设备时,以及从网格切换到全屏时,动画都很难看。
I have tried using 2 different collection layouts and switching between them, but the problem remains. 我尝试使用2种不同的集合布局并在它们之间进行切换,但是问题仍然存在。
If anyone has a sample application with this behavior or knows how to do it, I would be very thankful. 如果任何人都有具有这种行为的示例应用程序,或者知道如何做,我将非常感激。
This is what I have. 这就是我所拥有的。
@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];
}
I had an aim: CollectionView A have a grid of photos 3 in a row. 我的目标是:CollectionView A连续有3张照片的网格。 When i tap on one of photos it implements CollectionView B with horizontal scrolling and fullscreen images. 当我点击其中一张照片时,它会使用水平滚动和全屏图像实现CollectionViewB。
Here the code: 这里的代码:
1) CollectionView A only selectItem method: 1)CollectionView唯一的selectItem方法:
- (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];
}
Here self.Array
- is an Array of items (photos). 这里self.Array
是项目(照片)的数组。
2) CollectionView B - FullScreenViewController.h
2)CollectionView B- 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 "FullScreenViewController.h"
@implementation FullScreenViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection: UICollectionViewScrollDirectionHorizontal];
[layout setMinimumInteritemSpacing: 0.0f];
[layout setMinimumLineSpacing: 0.0f];
_collectionView = [[UICollectionView alloc] initWithFrame: self.view.frame collectionViewLayout: layout];
[_collectionView setDataSource: self];
[_collectionView setDelegate: self];
[_collectionView setPagingEnabled: YES];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor: [UIColor blackColor]];
[_collectionView scrollToItemAtIndexPath: [NSIndexPath indexPathForItem: _currentIndex.row inSection: 0] atScrollPosition: UICollectionViewScrollPositionCenteredHorizontally animated: NO];
[self.view addSubview:_collectionView];
}
#pragma mark - UICollectionView methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.arrayForFullScr count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath: indexPath];
CustomClass *information = [self.arrayForFullScr objectAtIndex: indexPath.row];
UIImageView *imgView = [[UIImageView alloc] initWithFrame: cell.frame];
[imgView sd_setImageWithPreviousCachedImageWithURL: information.url
placeholderImage: [UIImage imageNamed:@"placeholder_photo"]
options: 0
progress: nil
completed:nil];
imgView.contentMode = UIViewContentModeScaleAspectFit;
cell.backgroundView = imgView;
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height - 70);
}
@end
I used #import "SDWebImage/UIImageView+WebCache.h" to store images. 我使用#import“ SDWebImage / UIImageView + WebCache.h”来存储图像。 You can copy code and customize it. 您可以复制代码并对其进行自定义。
though @nik's answer is correct. 尽管@nik的答案是正确的。 I had an issue while scrolling to selected indexpath
. 滚动到选定的indexpath
。 It won't scroll correctly yo the selected index path. 所选索引路径无法正确滚动。
If you face the problem, I would recommend to put scrollToItemAtIndexPath:
method inside viewWillAppear
. 如果您遇到问题,建议您将scrollToItemAtIndexPath:
方法放在viewWillAppear
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.