-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathViewController.m
111 lines (85 loc) · 3.38 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// ViewController.m
// BalancedFlowLayoutDemo
//
// Created by Niels de Hoog on 08-10-13.
// Copyright (c) 2013 Niels de Hoog. All rights reserved.
//
#import "ViewController.h"
#import "ImageCell.h"
#import "NHLinearPartition.h"
#import "UIImage+Decompression.h"
#import "NHBalancedFlowLayout.h"
#define NUMBER_OF_IMAGES 24
#define HEADER_SIZE 100.0f
#define FOOTER_SIZE 100.0f
@interface ViewController () <NHBalancedFlowLayoutDelegate>
@property (nonatomic, strong) NSArray *images;
@end
@implementation ViewController
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 1; i <= NUMBER_OF_IMAGES; i++) {
NSString *imageName = [NSString stringWithFormat:@"photo-%02d.jpg", i];
[images addObject:[UIImage imageNamed:imageName]];
}
_images = [images copy];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NHBalancedFlowLayout *layout = (NHBalancedFlowLayout *)self.collectionViewLayout;
layout.stickyHeaders = YES;
layout.headerReferenceSize = CGSizeMake(HEADER_SIZE, HEADER_SIZE);
layout.footerReferenceSize = CGSizeMake(FOOTER_SIZE, FOOTER_SIZE);
}
#pragma mark - UICollectionViewFlowLayoutDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(NHBalancedFlowLayout *)collectionViewLayout preferredSizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return [[self.images objectAtIndex:indexPath.item] size];
}
#pragma mark - UICollectionView data source
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 3;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
return [self.images count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
cell.imageView.image = nil;
/**
* Decompress image on background thread before displaying it to prevent lag
*/
NSInteger rowIndex = indexPath.row;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image = [UIImage decodedImageWithImage:[self.images objectAtIndex:indexPath.item]];
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath *currentIndexPathForCell = [collectionView indexPathForCell:cell];
if (currentIndexPathForCell.row == rowIndex) {
cell.imageView.image = image;
}
});
});
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *view = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
}
return view;
}
@end