/
Demo1ViewController.m
454 lines (358 loc) · 14.5 KB
/
Demo1ViewController.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//
// ViewController.m
// GMGridView
//
// Created by Gulam Moledina on 11-10-09.
// Copyright (c) 2011 GMoledina.ca. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "Demo1ViewController.h"
#import "GMGridView.h"
#import "OptionsViewController.h"
#define NUMBER_ITEMS_ON_LOAD 250
//////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark ViewController (privates methods)
//////////////////////////////////////////////////////////////
@interface Demo1ViewController () <GMGridViewDataSource, GMGridViewSortingDelegate, GMGridViewTransformationDelegate, GMGridViewActionDelegate>
{
__weak GMGridView *_gmGridView;
UINavigationController *_optionsNav;
UIPopoverController *_optionsPopOver;
NSMutableArray *_data;
}
- (void)addMoreItem;
- (void)removeItem;
- (void)refreshItem;
- (void)presentInfo;
- (void)presentOptions:(UIBarButtonItem *)barButton;
- (void)optionsDoneAction;
@end
//////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark ViewController implementation
//////////////////////////////////////////////////////////////
@implementation Demo1ViewController
- (id)init
{
if ((self =[super init]))
{
self.title = @"Demo 1";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addMoreItem)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = 10;
UIBarButtonItem *removeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(removeItem)];
UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space2.width = 10;
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshItem)];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:addButton, space, removeButton, space2, refreshButton, nil];
UIBarButtonItem *optionsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(presentOptions:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:optionsButton, nil];
_data = [[NSMutableArray alloc] init];
for (int i = 0; i < NUMBER_ITEMS_ON_LOAD; i ++)
{
[_data addObject:[NSString stringWithFormat:@"%d", i]];
}
}
return self;
}
//////////////////////////////////////////////////////////////
#pragma mark controller events
//////////////////////////////////////////////////////////////
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
NSInteger spacing = INTERFACE_IS_PHONE ? 10 : 15;
GMGridView *gmGridView = [[GMGridView alloc] initWithFrame:self.view.bounds];
gmGridView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
gmGridView.backgroundColor = [UIColor clearColor];
[self.view addSubview:gmGridView];
_gmGridView = gmGridView;
_gmGridView.style = GMGridViewStyleSwap;
_gmGridView.itemSpacing = spacing;
_gmGridView.minEdgeInsets = UIEdgeInsetsMake(spacing, spacing, spacing, spacing);
_gmGridView.centerGrid = YES;
_gmGridView.actionDelegate = self;
_gmGridView.sortingDelegate = self;
_gmGridView.transformDelegate = self;
_gmGridView.dataSource = self;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
infoButton.frame = CGRectMake(self.view.bounds.size.width - 40,
self.view.bounds.size.height - 40,
40,
40);
infoButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
[infoButton addTarget:self action:@selector(presentInfo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:infoButton];
OptionsViewController *optionsController = [[OptionsViewController alloc] init];
optionsController.gridView = gmGridView;
optionsController.contentSizeForViewInPopover = CGSizeMake(400, 500);
_optionsNav = [[UINavigationController alloc] initWithRootViewController:optionsController];
if (INTERFACE_IS_PHONE)
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(optionsDoneAction)];
optionsController.navigationItem.rightBarButtonItem = doneButton;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
_gmGridView.mainSuperView = self.navigationController.view; //[UIApplication sharedApplication].keyWindow.rootViewController.view;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
//////////////////////////////////////////////////////////////
#pragma mark memory management
//////////////////////////////////////////////////////////////
//- (void)didReceiveMemoryWarning {
// [super didReceiveMemoryWarning];
//}
//////////////////////////////////////////////////////////////
#pragma mark orientation management
//////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
//////////////////////////////////////////////////////////////
#pragma mark GMGridViewDataSource
//////////////////////////////////////////////////////////////
- (NSInteger)numberOfItemsInGMGridView:(GMGridView *)gridView
{
return [_data count];
}
- (CGSize)sizeForItemsInGMGridView:(GMGridView *)gridView
{
if (INTERFACE_IS_PHONE)
{
return CGSizeMake(140, 110);
}
else
{
return CGSizeMake(230, 175);
}
}
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
//NSLog(@"Creating view indx %d", index);
CGSize size = [self sizeForItemsInGMGridView:gridView];
GMGridViewCell *cell = [gridView dequeueReusableCell];
if (!cell)
{
cell = [[GMGridViewCell alloc] init];
cell.deleteButtonIcon = [UIImage imageNamed:@"close_x.png"];
cell.deleteButtonOffset = CGPointMake(-15, -15);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
view.backgroundColor = [UIColor redColor];
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 *)[_data objectAtIndex:index];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:20];
[cell.contentView addSubview:label];
return cell;
}
- (void)GMGridView:(GMGridView *)gridView deleteItemAtIndex:(NSInteger)index
{
[_data removeObjectAtIndex:index];
}
//////////////////////////////////////////////////////////////
#pragma mark GMGridViewActionDelegate
//////////////////////////////////////////////////////////////
- (void)GMGridView:(GMGridView *)gridView didTapOnItemAtIndex:(NSInteger)position
{
NSLog(@"DId tap at index %d", position);
}
//////////////////////////////////////////////////////////////
#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 = [UIColor redColor];
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
{
NSObject *object = [_data objectAtIndex:oldIndex];
[_data removeObject:object];
[_data insertObject:object atIndex:newIndex];
}
- (void)GMGridView:(GMGridView *)gridView exchangeItemAtIndex:(NSInteger)index1 withItemAtIndex:(NSInteger)index2
{
[_data exchangeObjectAtIndex:index1 withObjectAtIndex:index2];
}
//////////////////////////////////////////////////////////////
#pragma mark DraggableGridViewTransformingDelegate
//////////////////////////////////////////////////////////////
- (CGSize)GMGridView:(GMGridView *)gridView sizeInFullSizeForCell:(GMGridViewCell *)cell
{
if (INTERFACE_IS_PHONE)
{
return CGSizeMake(310, 310);
}
else
{
return CGSizeMake(700, 530);
}
}
- (UIView *)GMGridView:(GMGridView *)gridView fullSizeViewForCell:(GMGridViewCell *)cell
{
UIView *fullView = [[UIView alloc] init];
fullView.backgroundColor = [UIColor yellowColor];
fullView.layer.masksToBounds = NO;
fullView.layer.cornerRadius = 8;
CGSize size = [self GMGridView:gridView sizeInFullSizeForCell:cell];
fullView.bounds = CGRectMake(0, 0, size.width, size.height);
UILabel *label = [[UILabel alloc] initWithFrame:fullView.bounds];
label.text = @"Fullscreen View";
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 = [UIColor redColor];
cell.contentView.layer.shadowOpacity = 0;
}
completion:nil];
}
- (void)GMGridView:(GMGridView *)gridView didEnterFullSizeForCell:(UIView *)cell
{
}
//////////////////////////////////////////////////////////////
#pragma mark private methods
//////////////////////////////////////////////////////////////
- (void)addMoreItem
{
// Example: adding object at the last position
NSString *newItem = [NSString stringWithFormat:@"%d", (int)(arc4random() % 1000)];
[_data addObject:newItem];
[_gmGridView insertObjectAtIndex:[_data count] - 1];
}
- (void)removeItem
{
// Example: removing last item
if ([_data count] > 0)
{
NSInteger index = [_data count] - 1;
[_gmGridView removeObjectAtIndex:index];
[_data removeObjectAtIndex:index];
}
}
- (void)refreshItem
{
// Example: reloading last item
if ([_data count] > 0)
{
int index = [_data count] - 1;
NSString *newMessage = [NSString stringWithFormat:@"%d", (arc4random() % 1000)];
[_data replaceObjectAtIndex:index withObject:newMessage];
[_gmGridView reloadObjectAtIndex:index];
}
}
- (void)presentInfo
{
NSString *info = @"Long-press an item and its color will change; letting you know that you can now move it around. \n\nUsing two fingers, pinch/drag/rotate an item; zoom it enough and you will enter the fullsize mode";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Info"
message:info
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
- (void)presentOptions:(UIBarButtonItem *)barButton
{
if (INTERFACE_IS_PHONE)
{
[self presentModalViewController:_optionsNav animated:YES];
}
else
{
if(![_optionsPopOver isPopoverVisible])
{
if (!_optionsPopOver)
{
_optionsPopOver = [[UIPopoverController alloc] initWithContentViewController:_optionsNav];
}
[_optionsPopOver presentPopoverFromBarButtonItem:barButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
[self optionsDoneAction];
}
}
}
- (void)optionsDoneAction
{
if (INTERFACE_IS_PHONE)
{
[self dismissModalViewControllerAnimated:YES];
}
else
{
[_optionsPopOver dismissPopoverAnimated:YES];
}
}
@end