Guest User

Untitled

a guest
Dec 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #import "LNCustomCollectionView.h"
  2.  
  3. // 单元格
  4. @interface PhotoGalleryCell : UICollectionViewCell
  5.  
  6. // 照片
  7. @property (strong, nonatomic) UIImageView *imageViewPhoto;
  8.  
  9. @end
  10.  
  11. // 视图
  12. @interface PhotoGalleryView : LNCustomCollectionView
  13.  
  14. @end
  15.  
  16.  
  17. #import "PhotoGalleryView.h"
  18. #import "KSPhotoBrowser.h"
  19.  
  20. // 单元格
  21. @implementation PhotoGalleryCell
  22.  
  23. - (instancetype)initWithFrame:(CGRect)frame {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. // 照片
  27. UIImageView *imageViewPhoto = [UIImageView new];
  28. imageViewPhoto.layer.masksToBounds = YES;
  29. imageViewPhoto.contentMode = UIViewContentModeScaleAspectFill;
  30. [self.contentView addSubview:imageViewPhoto];
  31. [imageViewPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.top.left.bottom.right.offset(0);
  33. }];
  34. self.imageViewPhoto = imageViewPhoto;
  35. }
  36. return self;
  37. }
  38.  
  39. @end
  40.  
  41. // 视图
  42. @implementation PhotoGalleryView
  43.  
  44. - (void)commonInit {
  45. [super commonInit];
  46.  
  47. UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
  48. layout.minimumInteritemSpacing = 5;
  49. layout.minimumLineSpacing = 5;
  50.  
  51. [self registerClass:[PhotoGalleryCell class] forCellWithReuseIdentifier:@"PhotoGalleryCell"];
  52. }
  53.  
  54. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  55. PhotoGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoGalleryCell" forIndexPath:indexPath];
  56. NSString *url = self.itemArray[indexPath.row];
  57. [cell.imageViewPhoto sd_setImageWithURL:FullImageUrl(url) placeholderImage:PLACEHOLDER_IMAGE];
  58. return cell;
  59. }
  60.  
  61. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  62. const NSInteger kItemNumOfRow = 3;
  63. UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
  64. NSInteger row = indexPath.row;
  65. CGFloat totalWidth = self.frame.size.width - layout.sectionInset.left - layout.sectionInset.right - layout.minimumInteritemSpacing * (kItemNumOfRow - 1);
  66. CGFloat width;
  67.  
  68. if (row % kItemNumOfRow) {
  69. width = roundf(totalWidth / kItemNumOfRow);
  70. } else {
  71. width = totalWidth - roundf(totalWidth / kItemNumOfRow) * (kItemNumOfRow - 1);
  72. }
  73.  
  74. return CGSizeMake(width, width);
  75. }
  76.  
  77. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  78.  
  79. NSMutableArray *items = @[].mutableCopy;
  80. for (int i = 0; i < self.itemArray.count; i++) {
  81. KSPhotoItem *item = [KSPhotoItem itemWithSourceView:nil imageUrl:FullImageUrl(self.itemArray[i])];
  82. [items addObject:item];
  83. }
  84. KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
  85. [browser showFromViewController:self.viewController];
  86. }
  87.  
  88. - (UIViewController *)viewController {
  89. id nextResponder = [self nextResponder];
  90. while (nextResponder != nil) {
  91. if ([nextResponder isKindOfClass:[UIViewController class]]) {
  92. UIViewController *vc = (UIViewController *)nextResponder;
  93. return vc;
  94. }
  95. nextResponder = [nextResponder nextResponder];
  96. }
  97. return nil;
  98. }
  99.  
  100. @end
Add Comment
Please, Sign In to add comment