-
Notifications
You must be signed in to change notification settings - Fork 0
/
KVMMainViewController.m
225 lines (188 loc) · 6.39 KB
/
KVMMainViewController.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
//
// KVMMainViewController.m
// Tenorion
//
// Created by Kimberly Merrill on 11/2/13.
// Copyright (c) 2013 kmerrill27. All rights reserved.
//
#import "KVMMainViewController.h"
@interface KVMMainViewController ()
@end
@implementation KVMMainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self setup];
}
return self;
}
- (void)setup
{
self.layerColors = [[NSMutableArray alloc] initWithObjects:[UIColor colorWithRed:0 green:238.0/255.0 blue:118.0/255.0 alpha:1], [UIColor whiteColor], [UIColor blueColor], [UIColor colorWithRed:238.0/255.0 green:48.0/255.0 blue:167.0/255.0 alpha:1], nil];
self.layerManager = [[KVMLayerManager alloc] initWithColumns:8 AndColors:self.layerColors AndTarget:self];
self.player = [[KVMPlayer alloc] initWithLayerManager:self.layerManager];
}
- (IBAction)didPan:(UIPanGestureRecognizer *)recognizer
{
CGPoint velocity = [recognizer velocityInView:self.view];
CGPoint location = [recognizer locationInView:self.view];
if (CGRectContainsPoint(self.arrowView.frame, location))
{
// Upward pan presents option controller
[recognizer setEnabled:NO];
[self panOptions:velocity];
}
else if (CGRectContainsPoint(self.switchBoardView.frame, location))
{
// Side pan moves between layers
[self panLayer:velocity];
}
}
- (void)panLayer:(CGPoint)velocity
{
if (velocity.x > 0)
{
// Right gesture
if ([self.layerManager hasPreviousLayer])
{
[self goToPreviousLayer];
}
}
else
{
// Left gesture
if ([self.layerManager hasNextLayer])
{
[self goToNextLayer];
}
else if ([self.layerManager canAddLayer])
{
[self addNewLayer];
}
}
}
- (void)panOptions:(CGPoint)velocity
{
// Up gesture
if (velocity.y < 0)
{
[self presentOptionsViewController];
}
}
- (void)goToPreviousLayer
{
KVMLayer* previousLayer = [self.layerManager getPreviousLayer];
[self.switchBoardView bringSubviewToFront:previousLayer];
[self tintElements:previousLayer.switchColor];
self.layerControl.selectedSegmentIndex = self.layerControl.selectedSegmentIndex - 1;
}
- (void)goToNextLayer
{
KVMLayer* nextLayer = [self.layerManager getNextLayer];
[self.switchBoardView bringSubviewToFront:nextLayer];
[self tintElements:nextLayer.switchColor];
self.layerControl.selectedSegmentIndex = self.layerControl.selectedSegmentIndex + 1;
}
- (void)addNewLayer
{
KVMLayer* layer = [self.layerManager addLayer];
[layer setupOptionsControllerWithDismissAction:@selector(dismissOptionsController) AndDeleteAction:@selector(deleteCurrentLayer)];
[self.switchBoardView addSubview:layer];
[self tintElements:layer.switchColor];
// Update layer segment control
int numLayers = [self.layerControl numberOfSegments];
[self.layerControl insertSegmentWithTitle:@"" atIndex:numLayers animated:YES];
self.layerControl.selectedSegmentIndex = numLayers;
}
- (void)deleteCurrentLayer
{
int layerToDelete = self.layerControl.selectedSegmentIndex;
BOOL isLastLayer = layerToDelete == [self.layerControl numberOfSegments] - 1;
[self.layerManager deleteLayerAtIndex:layerToDelete IsLastLayer:isLastLayer];
if (isLastLayer)
{
[self deleteLastLayer:layerToDelete];
}
else
{
[self deleteInBetweenLayer:layerToDelete];
}
[self tintElements:[self.layerManager getCurrentLayer].switchColor];
}
- (void)deleteInBetweenLayer:(int)layerToDelete
{
int currIndex = 0;
int numSegments = [self.layerControl numberOfSegments];
// Move all layers left one to keep segment control and layer manager consistent
for (int i = 0; i < numSegments; i++)
{
if (i != layerToDelete)
{
[self.layerControl removeSegmentAtIndex:currIndex animated:NO];
[self.layerControl insertSegmentWithTitle:@"" atIndex:currIndex animated:NO];
currIndex++;
}
}
[self.layerControl removeSegmentAtIndex:numSegments-1 animated:NO];
self.layerControl.selectedSegmentIndex = layerToDelete;
}
- (void)deleteLastLayer:(int)layerToDelete
{
[self.layerControl removeSegmentAtIndex:layerToDelete animated:YES];
if ([self.layerControl numberOfSegments] == 0)
{
// If only layer, re-add layer (must always have at least one layer)
[self addNewLayer];
}
else
{
self.layerControl.selectedSegmentIndex = layerToDelete-1;
}
}
- (void)tintElements:(UIColor *)color
{
// Change color of arrow and segment control graphic
self.layerControl.tintColor = color;
UIImage* imageForRendering = [self.arrowView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.arrowView.image = imageForRendering;
self.arrowView.tintColor = color;
}
- (void)presentOptionsViewController
{
CGRect mainrect = self.view.bounds;
CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);
KVMOptionsViewController* currOptionsController = [self.layerManager getCurrentLayer].optionsController;
[self.view addSubview:currOptionsController.view];
currOptionsController.view.frame = newRect;
[UIView animateWithDuration:0.5 animations:^{
currOptionsController.view.frame = mainrect;
} completion:nil];
}
- (void)dismissOptionsController
{
CGRect mainrect = self.view.bounds;
CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, 0);
KVMOptionsViewController* currOptionsController = [self.layerManager getCurrentLayer].optionsController;
[UIView animateWithDuration:0.5 animations:^{
currOptionsController.view.frame = newRect;
} completion:^(BOOL finished) {
[currOptionsController.view removeFromSuperview];
[currOptionsController setIsBeingDismissed:NO];
[self.panRecognizer setEnabled:YES];
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.layerControl removeAllSegments];
[self addNewLayer];
// Start main animation loop
[self.player startPlayback];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end