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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// MWGridViewController.m
// MWPhotoBrowser
//
// Created by Michael Waterfall on 08/10/2013.
//
//
#import "MWGridViewController.h"
#import "MWGridCell.h"
#import "MWPhotoBrowserPrivate.h"
#import "MWCommon.h"
@interface MWGridViewController () {
// Store margins for current setup
CGFloat _margin, _gutter, _marginL, _gutterL, _columns, _columnsL;
}
@end
@implementation MWGridViewController
- (id)init {
if ((self = [super initWithCollectionViewLayout:[UICollectionViewFlowLayout new]])) {
// Defaults
_columns = 3, _columnsL = 4;
_margin = 0, _gutter = 1;
_marginL = 0, _gutterL = 1;
// For pixel perfection...
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad
_columns = 6, _columnsL = 8;
_margin = 1, _gutter = 2;
_marginL = 1, _gutterL = 2;
} else if ([UIScreen mainScreen].bounds.size.height == 480) {
// iPhone 3.5 inch
_columns = 3, _columnsL = 4;
_margin = 0, _gutter = 1;
_marginL = 1, _gutterL = 2;
} else {
// iPhone 4 inch
_columns = 3, _columnsL = 5;
_margin = 0, _gutter = 1;
_marginL = 0, _gutterL = 2;
}
_initialContentOffset = CGPointMake(0, CGFLOAT_MAX);
}
return self;
}
#pragma mark - View
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[MWGridCell class] forCellWithReuseIdentifier:@"GridCell"];
self.collectionView.alwaysBounceVertical = YES;
self.collectionView.backgroundColor = [UIColor blackColor];
}
- (void)viewWillDisappear:(BOOL)animated {
// Cancel outstanding loading
NSArray *visibleCells = [self.collectionView visibleCells];
if (visibleCells) {
for (MWGridCell *cell in visibleCells) {
[cell.photo cancelAnyLoading];
}
}
[super viewWillDisappear:animated];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self performLayout];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
}
- (void)adjustOffsetsAsRequired {
// Move to previous content offset
if (_initialContentOffset.y != CGFLOAT_MAX) {
self.collectionView.contentOffset = _initialContentOffset;
[self.collectionView layoutIfNeeded]; // Layout after content offset change
}
// Check if current item is visible and if not, make it so!
if (_browser.numberOfPhotos > 0) {
NSIndexPath *currentPhotoIndexPath = [NSIndexPath indexPathForItem:_browser.currentIndex inSection:0];
NSArray *visibleIndexPaths = [self.collectionView indexPathsForVisibleItems];
BOOL currentVisible = NO;
for (NSIndexPath *indexPath in visibleIndexPaths) {
if ([indexPath isEqual:currentPhotoIndexPath]) {
currentVisible = YES;
break;
}
}
if (!currentVisible) {
[self.collectionView scrollToItemAtIndexPath:currentPhotoIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}
}
}
- (void)performLayout {
UINavigationBar *navBar = self.navigationController.navigationBar;
self.collectionView.contentInset = UIEdgeInsetsMake(navBar.frame.origin.y + navBar.frame.size.height + [self getGutter], 0, 0, 0);
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self.collectionView reloadData];
[self performLayout]; // needed for iOS 5 & 6
}
#pragma mark - Layout
- (CGFloat)getColumns {
if ((UIInterfaceOrientationIsPortrait(self.interfaceOrientation))) {
return _columns;
} else {
return _columnsL;
}
}
- (CGFloat)getMargin {
if ((UIInterfaceOrientationIsPortrait(self.interfaceOrientation))) {
return _margin;
} else {
return _marginL;
}
}
- (CGFloat)getGutter {
if ((UIInterfaceOrientationIsPortrait(self.interfaceOrientation))) {
return _gutter;
} else {
return _gutterL;
}
}
#pragma mark - Collection View
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return [_browser numberOfPhotos];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MWGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GridCell" forIndexPath:indexPath];
if (!cell) {
cell = [[MWGridCell alloc] init];
}
id <MWPhoto> photo = [_browser thumbPhotoAtIndex:indexPath.row];
cell.photo = photo;
cell.gridController = self;
cell.selectionMode = _selectionMode;
cell.isSelected = [_browser photoIsSelectedAtIndex:indexPath.row];
cell.index = indexPath.row;
UIImage *img = [_browser imageForPhoto:photo];
if (img) {
[cell displayImage];
} else {
[photo loadUnderlyingImageAndNotify];
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[_browser setCurrentPhotoIndex:indexPath.row];
[_browser hideGrid];
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
[((MWGridCell *)cell).photo cancelAnyLoading];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat margin = [self getMargin];
CGFloat gutter = [self getGutter];
CGFloat columns = [self getColumns];
CGFloat value = floorf(((self.view.bounds.size.width - (columns - 1) * gutter - 2 * margin) / columns));
return CGSizeMake(value, value);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return [self getGutter];
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return [self getGutter];
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
CGFloat margin = [self getMargin];
return UIEdgeInsetsMake(margin, margin, margin, margin);
}
@end