-
-
Save gliubc/52610df45f5f116d00d476de833da8b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "LNCustomCollectionView.h" | |
// 单元格 | |
@interface PhotoGalleryCell : UICollectionViewCell | |
// 照片 | |
@property (strong, nonatomic) UIImageView *imageViewPhoto; | |
@end | |
// 视图 | |
@interface PhotoGalleryCollectionView : LNCustomCollectionView | |
@end | |
#import "PhotoGalleryCollectionView.h" | |
#import "KSPhotoBrowser.h" | |
// 单元格 | |
@implementation PhotoGalleryCell | |
- (instancetype)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
// 照片 | |
UIImageView *imageViewPhoto = [UIImageView new]; | |
imageViewPhoto.layer.masksToBounds = YES; | |
imageViewPhoto.contentMode = UIViewContentModeScaleAspectFill; | |
[self.contentView addSubview:imageViewPhoto]; | |
[imageViewPhoto mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.top.left.bottom.right.offset(0); | |
}]; | |
self.imageViewPhoto = imageViewPhoto; | |
} | |
return self; | |
} | |
@end | |
// 视图 | |
@implementation PhotoGalleryCollectionView | |
- (void)commonInit { | |
[super commonInit]; | |
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout; | |
layout.minimumInteritemSpacing = 5; | |
layout.minimumLineSpacing = 5; | |
[self registerClass:[PhotoGalleryCell class] forCellWithReuseIdentifier:@"PhotoGalleryCell"]; | |
} | |
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { | |
PhotoGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoGalleryCell" forIndexPath:indexPath]; | |
NSString *url = self.itemArray[indexPath.row]; | |
[cell.imageViewPhoto sd_setImageWithURL:FullImageUrl(url) placeholderImage:PLACEHOLDER_IMAGE]; | |
return cell; | |
} | |
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { | |
const NSInteger kItemNumOfRow = 3; | |
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout; | |
NSInteger row = indexPath.row; | |
CGFloat totalWidth = self.frame.size.width - layout.sectionInset.left - layout.sectionInset.right - layout.minimumInteritemSpacing * (kItemNumOfRow - 1); | |
CGFloat width; | |
if (row % kItemNumOfRow) { | |
width = roundf(totalWidth / kItemNumOfRow); | |
} else { | |
width = totalWidth - roundf(totalWidth / kItemNumOfRow) * (kItemNumOfRow - 1); | |
} | |
return CGSizeMake(width, width); | |
} | |
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { | |
NSMutableArray *items = @[].mutableCopy; | |
for (int i = 0; i < self.itemArray.count; i++) { | |
KSPhotoItem *item = [KSPhotoItem itemWithSourceView:nil imageUrl:FullImageUrl(self.itemArray[i])]; | |
[items addObject:item]; | |
} | |
KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row]; | |
[browser showFromViewController:self.viewController]; | |
} | |
- (UIViewController *)viewController { | |
id nextResponder = [self nextResponder]; | |
while (nextResponder != nil) { | |
if ([nextResponder isKindOfClass:[UIViewController class]]) { | |
UIViewController *vc = (UIViewController *)nextResponder; | |
return vc; | |
} | |
nextResponder = [nextResponder nextResponder]; | |
} | |
return nil; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment