Skip to content
Navigation Menu
{{ message }}
forked from gmoledina/GMGridView
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOptionsViewController.m
More file actions
549 lines (455 loc) · 17.1 KB
/
Copy pathOptionsViewController.m
File metadata and controls
549 lines (455 loc) · 17.1 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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//
// OptionsViewController.m
// GMGridView
//
// Created by Gulam Moledina on 11-11-01.
// Copyright (c) 2011 GMoledina.ca. All rights reserved.
//
#import "OptionsViewController.h"
#import "GMGridViewLayoutStrategies.h"
#import "GMGridView.h"
// Sections
typedef enum {
OptionSectionGeneral = 0,
OptionSectionLayout,
OptionSectionSorting,
OptionSectionGestures,
OptionSectionDebug,
OptionSectionsCount
} OptionsTypeSections;
// General
typedef enum {
OptionTypeGeneralEditing = 0,
OptionGeneralCount
} OptionsTypeGeneral;
// Options layout
typedef enum {
OptionTypeLayoutStrategy = 0,
OptionsTypeLayoutSpacing,
OptionsTypeLayoutCenter,
OptionsTypeLayoutMinInsets,
OptionLayoutCount
} OptionsTypeLayout;
// Options sorting
typedef enum {
OptionTypeSortingStyle = 0,
OptionSortingCount
} OptionsTypeSorting;
// Options Gestures
typedef enum {
OptionTypeGesturesEditOnTap = 0,
OptionTypeGesturesDisableEditOnEmptySpaceTap,
OptionTypeGesturesCount
} OptionsTypeGestures;
// Options debug
typedef enum {
OptionTypeDebugGridBackground = 0,
OptionTypeDebugReload,
OptionDebugCount
} OptionsTypeDebug;
@interface OptionsViewController () <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>
{
__gm_weak UITableView *_tableView;
}
- (void)editingSwitchChanged:(UISwitch *)control;
- (void)sortStyleSegmentedControlChanged:(UISegmentedControl *)control;
- (void)layoutCenterSwitchChanged:(UISwitch *)control;
- (void)layoutSpacingSliderChanged:(UISlider *)control;
- (void)layoutInsetsSliderChanged:(UISlider *)control;
- (void)editOnTapSwitchChanged:(UISwitch *)control;
- (void)disableEditOnEmptySpaceTapSwitchChanged:(UISwitch *)control;
- (void)debugGridBackgroundSwitchChanged:(UISwitch *)control;
- (void)debugReloadButtonPressed:(UIButton *)control;
@end
//////////////////////////////////////////////////////////////
#pragma mark - Implementation
//////////////////////////////////////////////////////////////
@implementation OptionsViewController
@synthesize gridView;
//////////////////////////////////////////////////////////////
#pragma mark Constructor
//////////////////////////////////////////////////////////////
- (id)init
{
if ((self = [super init]))
{
self.title = @"Options";
}
return self;
}
//////////////////////////////////////////////////////////////
#pragma mark View lifecycle
//////////////////////////////////////////////////////////////
- (void)loadView
{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
self.view = tableView;
_tableView = tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
//////////////////////////////////////////////////////////////
#pragma mark Controller events
//////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
//////////////////////////////////////////////////////////////
#pragma mark UITableView datasource & delegates
//////////////////////////////////////////////////////////////
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 35;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 45;
if ([indexPath section] == OptionSectionLayout)
{
switch ([indexPath row])
{
case OptionTypeLayoutStrategy:
height = 160;
break;
}
}
return height;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return OptionSectionsCount;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = @"Unknown";
switch (section)
{
case OptionSectionGeneral:
title = @"General";
break;
case OptionSectionLayout:
title = @"Layout";
break;
case OptionSectionSorting:
title = @"Sorting";
break;
case OptionSectionGestures:
title = @"Gestures";
break;
case OptionSectionDebug:
title = @"Debug";
break;
}
return title;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger count = 0;
switch (section)
{
case OptionSectionGeneral:
count = OptionGeneralCount;
break;
case OptionSectionLayout:
count = OptionLayoutCount;
break;
case OptionSectionSorting:
count = OptionSortingCount;
break;
case OptionSectionGestures:
count = OptionTypeGesturesCount;
break;
case OptionSectionDebug:
count = OptionDebugCount;
break;
}
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
for (UIView* subview in cell.contentView.subviews) {
if ([subview isKindOfClass:[UIPickerView class]]) {
[subview removeFromSuperview];
}
}
}
if ([indexPath section] == OptionSectionGeneral)
{
switch ([indexPath row])
{
case OptionTypeGeneralEditing:
{
cell.detailTextLabel.text = @"Editing";
UISwitch *editingSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[editingSwitch addTarget:self action:@selector(editingSwitchChanged:) forControlEvents:UIControlEventValueChanged];
editingSwitch.on = self.gridView.isEditing;
cell.accessoryView = editingSwitch;
}
}
}
else if ([indexPath section] == OptionSectionLayout)
{
switch ([indexPath row])
{
case OptionTypeLayoutStrategy:
{
cell.detailTextLabel.text = @"";
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:cell.contentView.bounds];
pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
pickerView.showsSelectionIndicator = YES;
pickerView.delegate = self;
pickerView.dataSource = self;
switch ([self.gridView.layoutStrategy type])
{
case GMGridViewLayoutHorizontalPagedTTB:
[pickerView selectRow:3 inComponent:0 animated:YES];
break;
case GMGridViewLayoutHorizontalPagedLTR:
[pickerView selectRow:2 inComponent:0 animated:YES];
break;
case GMGridViewLayoutHorizontal:
[pickerView selectRow:1 inComponent:0 animated:YES];
break;
case GMGridViewLayoutVertical:
default:
[pickerView selectRow:0 inComponent:0 animated:YES];
break;
}
cell.contentView.clipsToBounds = YES;
[cell.contentView addSubview:pickerView];
break;
}
case OptionsTypeLayoutCenter:
{
cell.detailTextLabel.text = @"Center";
UISwitch *centerSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[centerSwitch addTarget:self action:@selector(layoutCenterSwitchChanged:) forControlEvents:UIControlEventValueChanged];
centerSwitch.on = self.gridView.centerGrid;
cell.accessoryView = centerSwitch;
break;
}
case OptionsTypeLayoutSpacing:
{
cell.detailTextLabel.text = @"Spacing";
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
[slider setMinimumValue:0];
[slider setMaximumValue:20];
[slider setValue:self.gridView.itemSpacing];
[slider setContinuous:NO];
[slider addTarget:self action:@selector(layoutSpacingSliderChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = slider;
break;
}
case OptionsTypeLayoutMinInsets:
{
cell.detailTextLabel.text = @"Edge insets";
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
[slider setMinimumValue:0];
[slider setMaximumValue:50];
[slider setValue:self.gridView.minEdgeInsets.top];
[slider setContinuous:NO];
[slider addTarget:self action:@selector(layoutInsetsSliderChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = slider;
break;
}
}
}
else if ([indexPath section] == OptionSectionSorting)
{
switch ([indexPath row])
{
case OptionTypeSortingStyle:
{
cell.detailTextLabel.text = @"Style";
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Swap", @"Push", nil]];
segmentedControl.frame = CGRectMake(0, 0, 150, 30);
[segmentedControl addTarget:self action:@selector(sortStyleSegmentedControlChanged:) forControlEvents:UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = (self.gridView.style == GMGridViewStylePush) ? 1 : 0;
cell.accessoryView = segmentedControl;
break;
}
}
}
else if ([indexPath section] == OptionSectionGestures)
{
switch ([indexPath row])
{
case OptionTypeGesturesEditOnTap:
{
cell.detailTextLabel.text = @"Edit on Long Tap";
UISwitch *editOnTapSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[editOnTapSwitch addTarget:self action:@selector(editOnTapSwitchChanged:) forControlEvents:UIControlEventValueChanged];
editOnTapSwitch.on = self.gridView.enableEditOnLongPress;
cell.accessoryView = editOnTapSwitch;
break;
}
case OptionTypeGesturesDisableEditOnEmptySpaceTap:
{
cell.detailTextLabel.text = @"Disable edit on empty tap";
UISwitch *disableEditOnEmptyTapSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[disableEditOnEmptyTapSwitch addTarget:self action:@selector(disableEditOnEmptySpaceTapSwitchChanged:) forControlEvents:UIControlEventValueChanged];
disableEditOnEmptyTapSwitch.on = self.gridView.enableEditOnLongPress;
cell.accessoryView = disableEditOnEmptyTapSwitch;
break;
}
}
}
else if ([indexPath section] == OptionSectionDebug)
{
switch ([indexPath row])
{
case OptionTypeDebugGridBackground:
{
cell.detailTextLabel.text = @"Grid background color";
UISwitch *backgroundSwitch = [[UISwitch alloc] init];
[backgroundSwitch addTarget:self action:@selector(debugGridBackgroundSwitchChanged:) forControlEvents:UIControlEventValueChanged];
backgroundSwitch.on = (self.gridView.backgroundColor != [UIColor clearColor]);
[backgroundSwitch sizeToFit];
cell.accessoryView = backgroundSwitch;
break;
}
case OptionTypeDebugReload:
{
cell.detailTextLabel.text = @"Reload from Datasource";
UIButton *reloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[reloadButton setReversesTitleShadowWhenHighlighted:YES];
[reloadButton setTitleColor:[UIColor redColor] forState:UIControlEventTouchUpInside];
[reloadButton setTitle:@"Reload" forState:UIControlStateNormal];
[reloadButton addTarget:self action:@selector(debugReloadButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[reloadButton sizeToFit];
cell.accessoryView = reloadButton;
break;
}
}
}
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
//////////////////////////////////////////////////////////////
#pragma mark UIPickerView delegate and datasource
//////////////////////////////////////////////////////////////
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (row)
{
case 1:
self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontal];
break;
case 2:
self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontalPagedLTR];
break;
case 3:
self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontalPagedTTB];
break;
case 0:
default:
self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutVertical];
break;
}
[self.gridView layoutSubviewsWithAnimation:GMGridViewItemAnimationFade];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 4;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title = nil;
switch (row) {
case 0:
title = @"Vertical strategy";
break;
case 1:
title = @"Horizontal strategy";
break;
case 2:
title = @"Horizontal paged LTR strategy";
break;
case 3:
title = @"Horizontal paged TTB strategy";
break;
default:
title = @"Unknown";
break;
}
return title;
}
//////////////////////////////////////////////////////////////
#pragma mark Control callbacks
//////////////////////////////////////////////////////////////
- (void)editingSwitchChanged:(UISwitch *)control
{
self.gridView.editing = control.on;
control.on = self.gridView.isEditing;
[self.gridView layoutSubviewsWithAnimation:GMGridViewItemAnimationFade];
}
- (void)sortStyleSegmentedControlChanged:(UISegmentedControl *)control
{
switch (control.selectedSegmentIndex)
{
case 1:
self.gridView.style = GMGridViewStylePush;
break;
case 0:
default:
self.gridView.style = GMGridViewStyleSwap;
break;
}
}
- (void)layoutCenterSwitchChanged:(UISwitch *)control
{
self.gridView.centerGrid = control.on;
[self.gridView layoutSubviewsWithAnimation:GMGridViewItemAnimationFade];
}
- (void)layoutSpacingSliderChanged:(UISlider *)control
{
self.gridView.itemSpacing = control.value;
[self.gridView layoutSubviewsWithAnimation:GMGridViewItemAnimationFade];
}
- (void)layoutInsetsSliderChanged:(UISlider *)control
{
self.gridView.minEdgeInsets = UIEdgeInsetsMake(control.value, control.value, control.value, control.value);
[self.gridView layoutSubviewsWithAnimation:GMGridViewItemAnimationFade];
}
- (void)editOnTapSwitchChanged:(UISwitch *)control
{
self.gridView.enableEditOnLongPress = control.on;
}
- (void)disableEditOnEmptySpaceTapSwitchChanged:(UISwitch *)control;
{
self.gridView.disableEditOnEmptySpaceTap = control.on;
}
- (void)debugGridBackgroundSwitchChanged:(UISwitch *)control
{
self.gridView.backgroundColor = control.on ? [UIColor lightGrayColor] : [UIColor clearColor];
}
- (void)debugReloadButtonPressed:(UIButton *)control
{
[self.gridView reloadData];
}
@end
You can’t perform that action at this time.
