Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "LNCustomCollectionView.h"
- // 单元格
- @interface PhotoGalleryCell : UICollectionViewCell
- // 照片
- @property (strong, nonatomic) UIImageView *imageViewPhoto;
- @end
- // 视图
- @interface PhotoGalleryView : LNCustomCollectionView
- @end
- #import "PhotoGalleryView.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 PhotoGalleryView
- - (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
Add Comment
Please, Sign In to add comment