forked from gmoledina/GMGridView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo2ViewController.m
More file actions
402 lines (323 loc) · 14.6 KB
/
Copy pathDemo2ViewController.m
File metadata and controls
402 lines (323 loc) · 14.6 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//
// Demo2ViewController.m
// GMGridView
//
// Created by Gulam Moledina on 11-11-10.
// Copyright (c) 2011 GMoledina.ca. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "Demo2ViewController.h"
#import "GMGridView.h"
#import "GMGridViewLayoutStrategies.h"
#import "OptionsViewController.h"
#import "OptionsViewController.h"
@interface Demo2ViewController () <GMGridViewDataSource, GMGridViewSortingDelegate, GMGridViewTransformationDelegate>
{
__gm_weak GMGridView *_gmGridView1;
__gm_weak GMGridView *_gmGridView2;
__gm_weak UIButton *_buttonOptionsGrid1;
__gm_weak UIButton *_buttonOptionsGrid2;
UIPopoverController *_popOverController;
UIViewController *_optionsController1;
UIViewController *_optionsController2;
}
- (void)computeViewFrames;
- (void)showOptionsFromButton:(UIButton *)button;
- (void)optionsDoneAction;
@end
//////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark ViewController implementation
//////////////////////////////////////////////////////////////
@implementation Demo2ViewController
- (id)init
{
if ((self = [super init]))
{
self.title = @"Demo 2";
}
return self;
}
//////////////////////////////////////////////////////////////
#pragma mark View lifecycle
//////////////////////////////////////////////////////////////
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
GMGridView *gmGridView = [[GMGridView alloc] initWithFrame:self.view.bounds];
gmGridView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
gmGridView.style = GMGridViewStylePush;
gmGridView.itemSpacing = 5;
gmGridView.minEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
gmGridView.centerGrid = YES;
gmGridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutVertical];
[self.view addSubview:gmGridView];
_gmGridView1 = gmGridView;
GMGridView *gmGridView2 = [[GMGridView alloc] initWithFrame:self.view.bounds];
gmGridView2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
gmGridView2.style = GMGridViewStylePush;
gmGridView2.itemSpacing = 5;
gmGridView2.minEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
gmGridView2.centerGrid = YES;
gmGridView2.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontal];
[self.view addSubview:gmGridView2];
_gmGridView2 = gmGridView2;
_buttonOptionsGrid1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_buttonOptionsGrid1 setTitle:@"Options Grid1" forState:UIControlStateNormal];
[_buttonOptionsGrid1 setReversesTitleShadowWhenHighlighted:YES];
[_buttonOptionsGrid1 sizeToFit];
_buttonOptionsGrid1.frame = CGRectMake(0, 0, _buttonOptionsGrid1.frame.size.width + 10, _buttonOptionsGrid1.frame.size.height + 10);
[_buttonOptionsGrid1 addTarget:self action:@selector(showOptionsFromButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_buttonOptionsGrid1];
_buttonOptionsGrid2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_buttonOptionsGrid2 setTitle:@"Options Grid2" forState:UIControlStateNormal];
[_buttonOptionsGrid2 setReversesTitleShadowWhenHighlighted:YES];
[_buttonOptionsGrid2 sizeToFit];
_buttonOptionsGrid2.frame = CGRectMake(0, 0, _buttonOptionsGrid2.frame.size.width + 10, _buttonOptionsGrid2.frame.size.height + 10);
[_buttonOptionsGrid2 addTarget:self action:@selector(showOptionsFromButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_buttonOptionsGrid2];
[self computeViewFrames];
}
- (void)viewDidLayoutSubviews
{
[self computeViewFrames];
}
- (void)viewDidLoad
{
[super viewDidLoad];
_gmGridView1.sortingDelegate = self;
_gmGridView1.transformDelegate = self;
_gmGridView1.dataSource = self;
_gmGridView2.sortingDelegate = self;
_gmGridView2.transformDelegate = self;
_gmGridView2.dataSource = self;
_gmGridView1.mainSuperView = self.view;
_gmGridView2.mainSuperView = self.view;
OptionsViewController *optionsController = [[OptionsViewController alloc] init];
optionsController.gridView = _gmGridView1;
optionsController.contentSizeForViewInPopover = CGSizeMake(400, 500);
_optionsController1 = [[UINavigationController alloc] initWithRootViewController:optionsController];
OptionsViewController *optionsController2 = [[OptionsViewController alloc] init];
optionsController2.gridView = _gmGridView2;
optionsController2.contentSizeForViewInPopover = CGSizeMake(400, 500);
_optionsController2 = [[UINavigationController alloc] initWithRootViewController:optionsController2];
if (INTERFACE_IS_PHONE)
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(optionsDoneAction)];
optionsController.navigationItem.rightBarButtonItem = doneButton;
UIBarButtonItem *doneButton2 = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(optionsDoneAction)];
optionsController2.navigationItem.rightBarButtonItem = doneButton2;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
//////////////////////////////////////////////////////////////
#pragma mark Controller events
//////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
//////////////////////////////////////////////////////////////
#pragma mark Privates
//////////////////////////////////////////////////////////////
- (void)computeViewFrames
{
CGSize itemSize = [self GMGridView:_gmGridView1 sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
CGSize minSize = CGSizeMake(itemSize.width + _gmGridView1.minEdgeInsets.right + _gmGridView1.minEdgeInsets.left,
itemSize.height + _gmGridView1.minEdgeInsets.top + _gmGridView1.minEdgeInsets.bottom);
CGRect frame1 = CGRectMake(10, 10, minSize.width, self.view.bounds.size.height - minSize.height - 30);
CGRect frame2 = CGRectMake(10, frame1.size.height + 20, self.view.bounds.size.width - 20 , minSize.height);
_gmGridView1.frame = frame1;
_gmGridView2.frame = frame2;
_buttonOptionsGrid1.frame = CGRectMake(frame1.origin.x + frame1.size.width + 5,
frame1.origin.y,
_buttonOptionsGrid1.frame.size.width,
_buttonOptionsGrid1.frame.size.height);
_buttonOptionsGrid2.frame = CGRectMake(frame2.origin.x + frame2.size.width - _buttonOptionsGrid2.frame.size.width,
frame2.origin.y - _buttonOptionsGrid2.frame.size.height - 5,
_buttonOptionsGrid2.frame.size.width,
_buttonOptionsGrid2.frame.size.height);
}
- (void)showOptionsFromButton:(UIButton *)button
{
UIViewController *optionsControllerToShow = button == _buttonOptionsGrid1 ? _optionsController1 : _optionsController2;
if (INTERFACE_IS_PHONE)
{
[self presentModalViewController:_optionsController1 animated:YES];
}
else
{
if(![_popOverController isPopoverVisible])
{
_popOverController = [[UIPopoverController alloc] initWithContentViewController:optionsControllerToShow];
[_popOverController presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
[self optionsDoneAction];
}
}
}
- (void)optionsDoneAction
{
if (INTERFACE_IS_PHONE)
{
[self dismissModalViewControllerAnimated:YES];
}
else
{
[_popOverController dismissPopoverAnimated:YES];
_popOverController = nil;
}
}
//////////////////////////////////////////////////////////////
#pragma mark GMGridViewDataSource
//////////////////////////////////////////////////////////////
- (NSInteger)numberOfItemsInGMGridView:(GMGridView *)gridView
{
return 50;
}
- (CGSize)GMGridView:(GMGridView *)gridView sizeForItemsInInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (INTERFACE_IS_PHONE)
{
return CGSizeMake(140, 110);
}
else
{
return CGSizeMake(230, 175);
}
}
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
if (!cell)
{
cell = [[GMGridViewCell alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
view.backgroundColor = (gridView == _gmGridView1) ? [UIColor purpleColor] : [UIColor greenColor];
view.layer.masksToBounds = NO;
view.layer.cornerRadius = 8;
view.layer.shadowColor = [UIColor grayColor].CGColor;
view.layer.shadowOffset = CGSizeMake(5, 5);
view.layer.shadowPath = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
view.layer.shadowRadius = 8;
cell.contentView = view;
}
[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
label.text = [NSString stringWithFormat:@"%d", index];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:20];
[cell.contentView addSubview:label];
return cell;
}
//////////////////////////////////////////////////////////////
#pragma mark GMGridViewSortingDelegate
//////////////////////////////////////////////////////////////
- (void)GMGridView:(GMGridView *)gridView didStartMovingCell:(GMGridViewCell *)cell
{
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
cell.contentView.backgroundColor = [UIColor orangeColor];
cell.contentView.layer.shadowOpacity = 0.7;
}
completion:nil
];
}
- (void)GMGridView:(GMGridView *)gridView didEndMovingCell:(GMGridViewCell *)cell
{
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
cell.contentView.backgroundColor = (gridView == _gmGridView1) ? [UIColor purpleColor] : [UIColor greenColor];
cell.contentView.layer.shadowOpacity = 0;
}
completion:nil
];
}
- (BOOL)GMGridView:(GMGridView *)gridView shouldAllowShakingBehaviorWhenMovingCell:(GMGridViewCell *)cell atIndex:(NSInteger)index
{
return YES;
}
- (void)GMGridView:(GMGridView *)gridView moveItemAtIndex:(NSInteger)oldIndex toIndex:(NSInteger)newIndex
{
// We dont care about this in this demo (see demo 1 for examples)
}
- (void)GMGridView:(GMGridView *)gridView exchangeItemAtIndex:(NSInteger)index1 withItemAtIndex:(NSInteger)index2
{
// We dont care about this in this demo (see demo 1 for examples)
}
//////////////////////////////////////////////////////////////
#pragma mark DraggableGridViewTransformingDelegate
//////////////////////////////////////////////////////////////
- (CGSize)GMGridView:(GMGridView *)gridView sizeInFullSizeForCell:(GMGridViewCell *)cell atIndex:(NSInteger)index inInterfaceOrientation:(UIInterfaceOrientation)orientation
{
CGSize viewSize = self.view.bounds.size;
return CGSizeMake(viewSize.width - 50, viewSize.height - 50);
}
- (UIView *)GMGridView:(GMGridView *)gridView fullSizeViewForCell:(GMGridViewCell *)cell atIndex:(NSInteger)index
{
UIView *fullView = [[UIView alloc] init];
fullView.backgroundColor = [UIColor yellowColor];
fullView.layer.masksToBounds = NO;
fullView.layer.cornerRadius = 8;
CGSize size = [self GMGridView:gridView sizeInFullSizeForCell:cell atIndex:index inInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
fullView.bounds = CGRectMake(0, 0, size.width, size.height);
UILabel *label = [[UILabel alloc] initWithFrame:fullView.bounds];
label.text = [NSString stringWithFormat:@"Fullscreen View for cell at index %d", index];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if (INTERFACE_IS_PHONE)
{
label.font = [UIFont boldSystemFontOfSize:15];
}
else
{
label.font = [UIFont boldSystemFontOfSize:20];
}
[fullView addSubview:label];
return fullView;
}
- (void)GMGridView:(GMGridView *)gridView didStartTransformingCell:(GMGridViewCell *)cell
{
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
cell.contentView.backgroundColor = [UIColor blueColor];
cell.contentView.layer.shadowOpacity = 0.7;
}
completion:nil];
}
- (void)GMGridView:(GMGridView *)gridView didEndTransformingCell:(GMGridViewCell *)cell
{
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
cell.contentView.backgroundColor = (gridView == _gmGridView1) ? [UIColor purpleColor] : [UIColor greenColor];
cell.contentView.layer.shadowOpacity = 0;
}
completion:nil];
}
- (void)GMGridView:(GMGridView *)gridView didEnterFullSizeForCell:(GMGridViewCell *)cell
{
}
@end