2013-09-24 3 views
1

UICollectionView 및 UICollectionViewFlowLayout을 사용하여 포토 갤러리를 만들려고합니다. 아이디어는 세로 스크롤을 사용하여 많은 사진을 그리드에 표시하는 것입니다. 사용자가 하나를 탭하면 페이지가 가로로 전체 화면이됩니다.UICollectionView 포토 갤러리를 전체 화면으로

내 문제는 사용자가 전체 화면 모드에서 장치를 돌릴 때 그리드에서 전체 화면으로 전환 할 때 애니메이션이 매우 못 생기는 것입니다.

두 가지 컬렉션 레이아웃을 사용하여 시도해 보았지만 그 사이를 전환했지만 문제가 남아 있습니다.

누구나이 동작을하는 샘플 응용 프로그램이 있거나이를 수행하는 방법을 알고있는 경우 매우 감사하게 생각합니다.

이것은 내가 가지고있는 것입니다. CollectionView A는 행에있는 사진 3의 격자를 가지고 :

@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]; 
} 

답변

1

를 사용합니다. 사진 중 하나를 탭하면 CollectionView B가 가로 스크롤과 전체 화면 이미지로 구현됩니다. 항목 (사진)의 배열 인 -

여기 self.Array
- (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];  
} 

:

1) CollectionView 만 selectItem 방법 : 다음 코드

.

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는 이미지를 저장하는 "SDWebImage/UIImageView에 + WebCache.h"# import를 사용했다. 코드를 복사하고 사용자 정의 할 수 있습니다.

0

@ nik 님의 답변이 맞습니다. 스크롤 할 때 문제가 발생하여 을 선택했습니다. 선택한 인덱스 경로로 올바르게 스크롤되지 않습니다.

문제가 발생하면 안에 scrollToItemAtIndexPath: 메서드를 넣는 것이 좋습니다.

관련 문제