forked from kokomo/NGVaryingGridView
/
NGVaryingGridView.m
367 lines (298 loc) · 13.9 KB
/
NGVaryingGridView.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// NGVaryingGridView.m
// NGVaryingGridView
//
// Created by Philip Messlehner on 19.04.12.
// Copyright (c) 2012 NOUS Wissensmanagement GmbH. All rights reserved.
//
#import "NGVaryingGridView.h"
@interface NGVaryingGridView () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *gridRects;
@property (nonatomic, strong) NSMutableDictionary *gridCells;
@property (nonatomic, strong) NSMutableSet *reuseableCells;
@property (nonatomic, strong) UIView *stickyViewForTopPosition;
@property (nonatomic, strong) UIView *stickyViewForLeftPosition;
@property (nonatomic) CGFloat stickyViewForTopPositionPadding;
@property (nonatomic) CGFloat stickyViewForLeftPositionPadding;
- (void)loadCellsInRect:(CGRect)rect;
- (void)handleTap:(UIGestureRecognizer *)gestureRecognizer;
- (void)updateStickyViewsPosition;
- (void)bringScrollingIndicatorsToFront;
@end
@implementation NGVaryingGridView
@synthesize scrollViewDelegate = _scrollViewDelegate;
@synthesize gridViewDelegate = _gridViewDelegate;
@synthesize scrollView = _scrollView;
@synthesize gridRects = _gridRects;
@synthesize gridCells = _gridCells;
@synthesize reuseableCells = _reuseableCells;
@synthesize maximumContentWidth = _maximumContentWidth;
@synthesize maximumContentHeight = _maximumContentHeight;
@synthesize stickyViewForTopPosition = _stickyViewForTopPosition;
@synthesize stickyViewForLeftPosition = _stickyViewForLeftPosition;
@synthesize stickyViewForLeftPositionPadding = _stickyViewForLeftPositionPadding;
@synthesize stickyViewForTopPositionPadding = _stickyViewForTopPositionPadding;
////////////////////////////////////////////////////////////////////////
#pragma mark - Life Cycle
////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.directionalLockEnabled = YES;
_gridCells = [NSMutableDictionary dictionary];
_reuseableCells = [NSMutableSet set];
_maximumContentHeight = CGFLOAT_MAX;
_maximumContentWidth = CGFLOAT_MAX;
[super addSubview:_scrollView];
_scrollView.delegate = self;
}
return self;
}
- (void)dealloc {
_scrollView.delegate = nil;
}
////////////////////////////////////////////////////////////////////////
#pragma mark - UIView
////////////////////////////////////////////////////////////////////////
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if (newSuperview != nil) {
[self reloadData];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
[self loadCellsInRect:self.visibleRect];
[self updateStickyViewsPosition];
[self bringScrollingIndicatorsToFront];
}
////////////////////////////////////////////////////////////////////////
#pragma mark - PVTVGridView
////////////////////////////////////////////////////////////////////////
- (void)setGridViewDelegate:(id<NGVaryingGridViewDelegate>)gridViewDelegate {
if (gridViewDelegate != _gridViewDelegate) {
_gridViewDelegate = gridViewDelegate;
[self reloadData];
}
}
- (void)reloadData {
if (_gridViewDelegate && self.superview != nil) {
CGFloat maxX = 0.f;
CGFloat maxY = 0.f;
self.gridRects = [self.gridViewDelegate rectsForCellsInGridView:self];
[self.gridCells.allValues makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.reuseableCells addObjectsFromArray:self.gridCells.allValues];
[self.gridCells removeAllObjects];
for (NSValue *rectValue in self.gridRects) {
CGRect rect = [rectValue CGRectValue];
maxX = MAX(maxX, rect.origin.x + rect.size.width);
maxY = MAX(maxY, rect.origin.y + rect.size.height);
}
maxX = MIN(maxX, self.maximumContentWidth);
maxY = MIN(maxY, self.maximumContentHeight);
self.scrollView.contentSize = CGSizeMake(maxX, maxY);
[self loadCellsInRect:self.visibleRect];
}
}
- (UIView *)gridCellWithCGPoint:(CGPoint)point {
for (UIView *view in self.gridCells.allValues) {
if (CGRectContainsPoint(view.frame, point)) {
return view;
}
}
return nil;
}
- (void)addOverlayView:(UIView *)overlayView {
[super addSubview:overlayView];
}
- (void)setStickyView:(UIView *)view lockPosition:(NGVaryingGridViewLockPosition)lockPosition {
switch (lockPosition) {
case NGVaryingGridViewLockPositionTop:
[self.stickyViewForTopPosition removeFromSuperview];
self.stickyViewForTopPositionPadding = view.frame.origin.y;
self.stickyViewForTopPosition = view;
break;
case NGVaryingGridViewLockPositionLeft:
[self.stickyViewForLeftPosition removeFromSuperview];
self.stickyViewForLeftPositionPadding = view.frame.origin.x;
self.stickyViewForLeftPosition = view;
break;
default:
break;
}
[self.scrollView addSubview:view];
}
////////////////////////////////////////////////////////////////////////
#pragma mark - PVTVGridView (Private Methods)
////////////////////////////////////////////////////////////////////////
- (void)handleTap:(UIGestureRecognizer *)gestureRecognizer {
if ([self.gridViewDelegate respondsToSelector:@selector(gridView:didSelectCell:index:)]) {
[self.gridViewDelegate gridView:self didSelectCell:gestureRecognizer.view index:[self.gridRects indexOfObject:[NSValue valueWithCGRect:gestureRecognizer.view.frame]]];
}
}
- (void)loadCellsInRect:(CGRect)rect {
NSUInteger index = 0;
NSMutableDictionary *usedCells = [NSMutableDictionary dictionary];
for (NSValue *rectValue in self.gridRects) {
CGRect rectOfValue = [rectValue CGRectValue];
if (!CGRectIsEmpty(CGRectIntersection(rect, rectOfValue))) {
UIView *gridViewCell = [self.gridCells objectForKey:rectValue];
if (gridViewCell == nil) {
gridViewCell = [self.gridViewDelegate gridView:self viewForCellWithRect:rectOfValue index:index];
[gridViewCell addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]];
[self.gridCells setObject:gridViewCell forKey:rectValue];
}
[usedCells setObject:gridViewCell forKey:rectValue];
[self.scrollView addSubview:gridViewCell];
}
index ++;
}
// Move unused Cells to reusableCells
NSMutableDictionary *unusedCells = [NSMutableDictionary dictionaryWithDictionary:self.gridCells];
[unusedCells removeObjectsForKeys:usedCells.allKeys];
[self.gridCells removeObjectsForKeys:unusedCells.allKeys];
[self.reuseableCells addObjectsFromArray:unusedCells.allValues];
}
- (UIView *)dequeueReusableCell {
UIView *reusableCell = [self.reuseableCells anyObject];
if (reusableCell) {
if ([self.gridViewDelegate respondsToSelector:@selector(gridView:willPrepareCellForReuse:)]) {
[self.gridViewDelegate gridView:self willPrepareCellForReuse:reusableCell];
}
[self.reuseableCells removeObject:reusableCell];
}
return reusableCell;
}
- (void)updateStickyViewsPosition {
self.stickyViewForTopPosition.frame = CGRectMake(self.stickyViewForTopPosition.frame.origin.x, self.scrollView.contentOffset.y + self.stickyViewForTopPositionPadding, self.stickyViewForTopPosition.frame.size.width, self.stickyViewForTopPosition.frame.size.height);
self.stickyViewForLeftPosition.frame = CGRectMake(self.scrollView.contentOffset.x + self.stickyViewForLeftPositionPadding, self.stickyViewForLeftPosition.frame.origin.y, self.stickyViewForLeftPosition.frame.size.width, self.stickyViewForLeftPosition.frame.size.height);
[self.scrollView bringSubviewToFront:self.stickyViewForTopPosition];
[self.scrollView bringSubviewToFront:self.stickyViewForLeftPosition];
}
- (void)bringScrollingIndicatorsToFront {
// ScrollIndicator will hide underneath view when you are adding Subviews while Scrolling
if (self.showsHorizontalScrollIndicator) {
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = YES;
}
if (self.showsVerticalScrollIndicator) {
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = YES;
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark - UIScrolLView Forwarding
////////////////////////////////////////////////////////////////////////
- (void)scrollToGridCell:(UIView *)cell animated:(BOOL)animated {
[self.scrollView scrollRectToVisible:cell.frame animated:animated];
}
- (CGRect)visibleRect {
CGRect visibleRect;
visibleRect.origin = self.scrollView.contentOffset;
visibleRect.size = self.scrollView.bounds.size;
float scale = 1.0 / self.scrollView.zoomScale;
visibleRect.origin.x *= scale;
visibleRect.origin.y *= scale;
visibleRect.size.width *= scale;
visibleRect.size.height *= scale;
return visibleRect;
}
- (void)addSubview:(UIView *)view {
[self.scrollView addSubview:view];
}
- (BOOL)isDirectionalLockEnabled {
return self.scrollView.isDirectionalLockEnabled;
}
- (void)setDirectionalLockEnabled:(BOOL)directionalLockEnabled {
self.scrollView.directionalLockEnabled = directionalLockEnabled;
}
- (void)setShowsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator {
self.scrollView.showsVerticalScrollIndicator = showsVerticalScrollIndicator;
}
- (BOOL)showsVerticalScrollIndicator {
return self.scrollView.showsVerticalScrollIndicator;
}
- (void)setShowsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator {
self.scrollView.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator;
}
- (BOOL)showsHorizontalScrollIndicator {
return self.scrollView.showsHorizontalScrollIndicator;
}
////////////////////////////////////////////////////////////////////////
#pragma mark - UIScrollViewDelegate
////////////////////////////////////////////////////////////////////////
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
[self.scrollViewDelegate scrollViewDidScroll:scrollView];
}
[self loadCellsInRect:self.visibleRect];
[self updateStickyViewsPosition];
[self bringScrollingIndicatorsToFront];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewDidZoom:)]) {
[self.scrollViewDelegate scrollViewDidZoom:scrollView];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
[self.scrollViewDelegate scrollViewWillBeginDragging:scrollView];
}
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) {
[self.scrollViewDelegate scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)]) {
[self.scrollViewDelegate scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewWillBeginDecelerating:)]) {
[self.scrollViewDelegate scrollViewWillBeginDecelerating:scrollView];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) {
[self.scrollViewDelegate scrollViewDidEndDecelerating:scrollView];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewDidEndScrollingAnimation:)]) {
[self.scrollViewDelegate scrollViewDidEndScrollingAnimation:scrollView];
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:@selector(viewForZoomingInScrollView:)]) {
return [self.scrollViewDelegate viewForZoomingInScrollView:scrollView];
}
return nil;
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewWillBeginZooming:withView:)]) {
[self.scrollViewDelegate scrollViewWillBeginZooming:scrollView withView:view];
}
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewDidEndZooming:withView:atScale:)]) {
[self.scrollViewDelegate scrollViewDidEndZooming:scrollView withView:view atScale:scale];
}
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewShouldScrollToTop:)]) {
return [self.scrollViewDelegate scrollViewShouldScrollToTop:scrollView];
}
return YES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
if ([self.scrollViewDelegate respondsToSelector:@selector(scrollViewDidScrollToTop:)]) {
[self.scrollViewDelegate scrollViewDidScrollToTop:scrollView];
}
}
@end