-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuSliderViewController.m
More file actions
226 lines (187 loc) · 9.26 KB
/
MenuSliderViewController.m
File metadata and controls
226 lines (187 loc) · 9.26 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
//
// MenuSliderViewController.m
// TwitterClient
//
// Created by Jessica Ko on 4/3/14.
// Copyright (c) 2014 Jessica Ko. All rights reserved.
//
#import "MenuSliderViewController.h"
#import "LeftNavViewController.h"
#import "MyProfileViewController.h"
#import "HomeViewController.h"
@interface MenuSliderViewController ()
@property (nonatomic,strong) UIViewController<MenuProtocol> *rootViewController;
@property (nonatomic,strong) UIViewController<MenuProtocol> *leftViewController;
@property (nonatomic,strong) UIViewController<MenuProtocol> *profileController;
@property (nonatomic,strong) UIViewController<MenuProtocol> *mentionsController;
@property (nonatomic,strong) UIView *overlay;
@property (nonatomic,assign) BOOL leftViewVisible;
-(void)onTouch:(id)sender;
@end
@implementation MenuSliderViewController
- (instancetype)initWithRootViewController:(UIViewController<MenuProtocol> *)rootViewController leftViewController:(UIViewController<MenuProtocol> *)leftViewController profileController:(UIViewController<MenuProtocol> *)profileController mentionsController:(UIViewController<MenuProtocol> *)mentionsController {
self = [super init];
if (self) {
_rootViewController = rootViewController;
_leftViewController = leftViewController;
_profileController = profileController;
_mentionsController = mentionsController;
_overlay = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0.f, 60.f, self.view.frame.size.height)];
[_overlay setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:_overlay];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:self.rootViewController.view];
[self addChildViewController:self.rootViewController];
[self.rootViewController didMoveToParentViewController:self];
self.rootViewController.delegate = self;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragOutLeftView:)];
[self.rootViewController.view addGestureRecognizer:panRecognizer];
UIPanGestureRecognizer *panRecognizerOnProfile = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragOutLeftView:)];
[self.profileController.view addGestureRecognizer:panRecognizerOnProfile];
UIPanGestureRecognizer *panRecognizerOnMentions = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragOutLeftView:)];
[self.mentionsController.view addGestureRecognizer:panRecognizerOnMentions];
}
- (void)dragOutLeftView:(UIPanGestureRecognizer *)recognizer {
if([recognizer state] == UIGestureRecognizerStateBegan)
{
[self toggleLeftMenu];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIView *)leftView {
if (![self.rootViewController.childViewControllers containsObject:self.leftViewController]) {
[self.view addSubview:self.leftViewController.view];
[self addChildViewController:self.leftViewController];
[self.leftViewController didMoveToParentViewController:self];
}
return self.leftViewController.view;
}
- (UIView *)profileView {
if (![self.rootViewController.childViewControllers containsObject:self.profileController]) {
[self.view addSubview:self.profileController.view];
[self addChildViewController:self.profileController];
[self.profileController didMoveToParentViewController:self];
}
return self.profileController.view;
}
- (UIView *)mentionsView {
if (![self.rootViewController.childViewControllers containsObject:self.mentionsController]) {
[self.view addSubview:self.mentionsController.view];
[self addChildViewController:self.mentionsController];
[self.mentionsController didMoveToParentViewController:self];
}
return self.mentionsController.view;
}
- (void)toggleLeftMenu {
if (self.leftViewVisible) {
self.rootViewController.delegate = self;
[self resetMenu];
} else {
self.leftViewController.delegate = self;
[self.view sendSubviewToBack:[self leftView]];
[self.view bringSubviewToFront:self.overlay];
self.overlay.frame = CGRectMake(self.view.frame.size.width - 60.f, 0.f, 60.f, self.view.frame.size.height);
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onTouch:)];
UITapGestureRecognizer *tabRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouch:)];
[self.overlay addGestureRecognizer:panRecognizer];
[self.overlay addGestureRecognizer:tabRecognizer];
[UIView animateWithDuration:0.9f
delay:0.0f
usingSpringWithDamping:0.9f
initialSpringVelocity:10.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect currentFrame = self.rootViewController.view.frame;
self.rootViewController.view.frame = CGRectOffset(currentFrame,(self.view.frame.size.width - 60.0f), 0.0f);
self.profileController.view.frame = CGRectOffset(currentFrame,(self.view.frame.size.width - 60.0f), 0.0f);
self.mentionsController.view.frame = CGRectOffset(currentFrame,(self.view.frame.size.width - 60.0f), 0.0f);
CALayer *layer = self.rootViewController.view.layer;
layer.shadowOffset = CGSizeMake(1, 1);
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowRadius = 4.0f;
layer.shadowOpacity = 0.80f;
layer.shadowPath = [[UIBezierPath bezierPathWithRect:layer.bounds] CGPath];
}
completion:^(BOOL finished) {
if (finished) {
self.leftViewVisible = YES;
}
}];
}
}
- (void)onTouch:(id)sender {
[self resetMenu];
self.overlay.frame = CGRectMake(self.view.frame.size.width, 0.f, 60.f, self.view.frame.size.height);
}
- (void)loadProfile {
NSLog(@"load profile");
[self resetMenu];
self.profileController.delegate = self;
[self.view bringSubviewToFront:[self profileView]];
[UIView animateWithDuration:0.9f
delay:0.0f
usingSpringWithDamping:0.9f
initialSpringVelocity:10.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect currentFrame = self.rootViewController.view.frame;
self.profileController.view.frame = CGRectOffset(currentFrame,0.0f, 0.0f);
}
completion:^(BOOL finished) {
if (finished) {
self.leftViewVisible = NO;
}
}];
}
- (void)loadMentions {
NSLog(@"load mentions");
[self resetMenu];
self.mentionsController.delegate = self;
[self.view bringSubviewToFront:[self mentionsView]];
[UIView animateWithDuration:0.9f
delay:0.0f
usingSpringWithDamping:0.9f
initialSpringVelocity:10.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect currentFrame = self.rootViewController.view.frame;
self.mentionsController.view.frame = CGRectOffset(currentFrame,0.0f, 0.0f);
}
completion:^(BOOL finished) {
if (finished) {
self.leftViewVisible = NO;
}
}];
}
- (void)loadHome {
NSLog(@"load home");
self.rootViewController.delegate = self;
[self.view sendSubviewToBack:[self profileView]];
[self.view sendSubviewToBack:[self mentionsView]];
[self resetMenu];
}
- (void)resetMenu {
[UIView animateWithDuration:0.5f
delay:0.0f
usingSpringWithDamping:0.9f
initialSpringVelocity:0.8f
options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect currentFrame = self.rootViewController.view.frame;
self.rootViewController.view.frame = CGRectMake(0.0f, 0.0f, currentFrame.size.width, currentFrame.size.height);
self.profileController.view.frame = CGRectMake(0.0f, 0.0f, currentFrame.size.width, currentFrame.size.height);
self.mentionsController.view.frame = CGRectMake(0.0f, 0.0f, currentFrame.size.width, currentFrame.size.height);
} completion:^(BOOL finished) {
if (finished) {
self.leftViewVisible = NO;
}
}];
}
@end