-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTNumpukViewController.m
More file actions
316 lines (262 loc) · 11.7 KB
/
DTNumpukViewController.m
File metadata and controls
316 lines (262 loc) · 11.7 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
//
// DTNumpukViewController.m
//
// Created by Didats on 5/24/13.
// Copyright (c) 2013 Didats Triadi. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "DTNumpukViewController.h"
@interface DTNumpukViewController () {
NSInteger defaultTag;
UIView *firstView, *secondView, *thirdView;
CGSize screenDimension;
NSArray *arrViews, *arrViewControllers;
UIPanGestureRecognizer *panGesture1, *panGesture2;
CGFloat firstPosition, firstTouchX, firstTouchY;
UIView *selectedView;
float movingAnimation;
}
@end
@implementation DTNumpukViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// screen dimension
screenDimension = [UIScreen mainScreen].applicationFrame.size;
// if ios7, +20 on the height
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
screenDimension = CGSizeMake(screenDimension.width, screenDimension.height + 20);
}
// set current state and status
currentState = DTNumpukStateSecondFocused;
self.numpukState = currentState;
firstTouchX = 0.0;
firstTouchY = 0.0;
movingAnimation = 0.1;
defaultTag = 1000;
// create view with full screen size
firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenDimension.width, screenDimension.height)];
secondView = [[UIView alloc] initWithFrame:CGRectMake(50, 0, screenDimension.width, screenDimension.height)];
thirdView = [[UIView alloc] initWithFrame:CGRectMake(270, 0, screenDimension.width, screenDimension.height)];
// add them to the array so it will be easier to manage them.
// ordered decending
arrViews = @[firstView, secondView, thirdView];
arrViewControllers = @[self.firstViewController, self.secondViewController, self.thirdViewController];
NSArray *bgColors = @[UIColorFromRGB(0x242424),UIColorFromRGB(0x242424), UIColorFromRGB(0x242424)];
// make all of them black background with corner radius
for (int i = 0; i < [arrViews count]; i++) {
UIView *currentView = (UIView *)[arrViews objectAtIndex:i];
[currentView setBackgroundColor:[bgColors objectAtIndex:i]];
[[currentView layer] setShadowColor:[UIColor blackColor].CGColor];
[[currentView layer] setShadowOpacity:1];
[[currentView layer] setShadowOffset:CGSizeMake(1.0, 1.0)];
CGRect shadowPath = CGRectMake(currentView.bounds.origin.x - 5, currentView.bounds.origin.y, currentView.bounds.size.width, currentView.bounds.size.height);
[[currentView layer] setShadowPath:[UIBezierPath bezierPathWithRect:shadowPath].CGPath];
// set tag
[currentView setTag:i+1];
// add them to the root view
[self.view addSubview:[arrViews objectAtIndex:i]];
// add each view to view controller
UIViewController *objViewController = [arrViewControllers objectAtIndex:i];
[self addChildViewController:objViewController];
[objViewController.view setFrame:CGRectMake(0, 0, screenDimension.width, screenDimension.height)];
[objViewController.view.layer setCornerRadius:6.0];
[objViewController.view.layer setMasksToBounds:YES];
[objViewController.view setBackgroundColor:[bgColors objectAtIndex:i]];
[currentView addSubview:objViewController.view];
// create button to enable the clicked area
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, screenDimension.width/3, screenDimension.height)];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setHidden:(i == 1) ? YES : NO];
[button setTag:1000];
[currentView addSubview:button];
}
// add gesture to second view and the third view
[self addGestureToView:secondView];
[self addGestureToView:thirdView];
}
#pragma mark - Public Methods
-(void) showSecondViewWithViewController:(UIViewController *)viewController {
// if view controller nil, just go to the state
if (viewController == nil) {
[self animateViewToState:DTNumpukStateSecondFocused];
}
else {
// need to remove and add new view controller
UIViewController *previous = self.secondViewController;
if (previous != viewController) {
[previous willMoveToParentViewController:nil];
[previous.view removeFromSuperview];
[previous removeFromParentViewController];
if (viewController) {
[self addChildViewController:viewController];
[secondView addSubview:viewController.view];
// set the button on top of it
[secondView bringSubviewToFront:[secondView viewWithTag:1000]];
[self animateViewToState:DTNumpukStateSecondFocused];
}
}
}
}
-(void) showThirdViewWithViewController:(UIViewController *) viewController {
if (viewController == nil) {
[self animateViewToState:DTNumpukStateThirdFocused];
}
else {
// need to remove and add new view controller
UIViewController *previous = self.thirdViewController;
if (previous != viewController) {
[previous willMoveToParentViewController:nil];
[previous.view removeFromSuperview];
[previous removeFromParentViewController];
if (viewController) {
[self addChildViewController:viewController];
[thirdView addSubview:viewController.view];
// set the button on top of it
[thirdView bringSubviewToFront:[thirdView viewWithTag:1000]];
[self animateViewToState:DTNumpukStateThirdFocused];
}
}
}
}
#pragma mark - Private Methods
-(void) animateViewToState:(DTNumpukState)toState {
// focus on the first view
if(toState == DTNumpukStateFirstFocused) {
[UIView animateWithDuration:movingAnimation animations:^{
[secondView setFrame:CGRectMake(220, 0, screenDimension.width, screenDimension.height)];
[thirdView setFrame:CGRectMake(270, 0, screenDimension.width, screenDimension.height)];
} completion:^(BOOL finished) {
// show the button
[[firstView viewWithTag:1000] setHidden:YES];
[[secondView viewWithTag:1000] setHidden:NO];
[[thirdView viewWithTag:1000] setHidden:NO];
// call delegate
if ([self.delegate respondsToSelector:@selector(firstViewFocused)]) {
[self.delegate firstViewFocused];
}
}];
}
// focus on the second view
else if(toState == DTNumpukStateSecondFocused) {
[UIView animateWithDuration:movingAnimation animations:^{
[secondView setFrame:CGRectMake(50, 0, screenDimension.width, screenDimension.height)];
[thirdView setFrame:CGRectMake(270, 0, screenDimension.width, screenDimension.height)];
} completion:^(BOOL finished) {
// show the button
[[firstView viewWithTag:1000] setHidden:NO];
[[secondView viewWithTag:1000] setHidden:YES];
[[thirdView viewWithTag:1000] setHidden:NO];
// call delegate
if ([self.delegate respondsToSelector:@selector(secondViewFocused)]) {
[self.delegate secondViewFocused];
}
}];
}
// focus on the third view
else if(toState == DTNumpukStateThirdFocused) {
[UIView animateWithDuration:movingAnimation animations:^{
[secondView setFrame:CGRectMake(50, 0, screenDimension.width, screenDimension.height)];
[thirdView setFrame:CGRectMake(0, 0, screenDimension.width, screenDimension.height)];
} completion:^(BOOL finished) {
// show the button
[[firstView viewWithTag:1000] setHidden:NO];
[[secondView viewWithTag:1000] setHidden:YES];
[[thirdView viewWithTag:1000] setHidden:YES];
// call delegate
if ([self.delegate respondsToSelector:@selector(thirdViewFocused)]) {
[self.delegate thirdViewFocused];
}
}];
}
}
-(void) animateViewWithMovingFactor:(float) factor andView:(UIView *) view {
if (view.tag == 2) {
// user drag the second view
if (view.frame.origin.x >= 150) {
// focus on the first view
[self animateViewToState:DTNumpukStateFirstFocused];
}
else {
// go back to focus on the second view
[self animateViewToState:DTNumpukStateSecondFocused];
}
}
else if (view.tag == 3) {
if (view.frame.origin.x >= 160) {
[self animateViewToState:DTNumpukStateSecondFocused];
}
else {
[self animateViewToState:DTNumpukStateThirdFocused];
}
}
}
-(void) addGestureToView: (UIView *) view {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panGesture.delegate = self;
panGesture.maximumNumberOfTouches = 100;
panGesture.minimumNumberOfTouches = 1;
[view addGestureRecognizer:panGesture];
}
-(void) handlePan:(UIGestureRecognizer *)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
if ([sender isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)sender;
CGPoint translatedPoint = [pan translationInView:self.view];
// get the position on the first touch
if (sender.state == UIGestureRecognizerStateBegan) {
firstTouchY = pan.view.center.y;
firstTouchX = pan.view.center.x;
}
// set center based on the dragging position
translatedPoint = CGPointMake(firstTouchX + translatedPoint.x, firstTouchY);
[pan.view setCenter:translatedPoint];
if (sender.state == UIGestureRecognizerStateEnded) {
float slideFactor = movingAnimation;
slideFactor = slideFactor * pan.view.frame.origin.x;
[self animateViewWithMovingFactor:slideFactor andView:pan.view];
}
else if (sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed) {
float slideFactor = movingAnimation;
slideFactor = slideFactor * pan.view.frame.origin.x;
[self animateViewWithMovingFactor:slideFactor andView:pan.view];
}
}
}
-(void) buttonClicked:(id) sender {
// tap on the first view
if ([sender superview].tag == 1) {
[self animateViewToState:DTNumpukStateFirstFocused];
}
// tap on the second view
else if([sender superview].tag == 2) {
[self animateViewToState:DTNumpukStateSecondFocused];
}
// tap on the third view
else if([sender superview].tag == 3) {
[self animateViewToState:DTNumpukStateThirdFocused];
}
}
-(void) removeGestureFromView:(UIView *) view {
if ([view.gestureRecognizers count] > 0) {
for (UIGestureRecognizer *recognizer in view.gestureRecognizers) {
[view removeGestureRecognizer:recognizer];
}
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end