变更记录

序号 录入时间 录入人 备注
1 2015-04-22 Alfred Jiang -
2 2015-12-23 Alfred Jiang -

方案名称

UINavigationController - 使用 SlideNavigationController 实现侧滑需求

关键字

UINavigationController \ 侧滑显示

需求场景

  1. 需要实现侧滑需求时

参考链接

  1. GitHub - SlideNavigationController

详细内容

######1. SlideNavigationController.h

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
//
// SlideNavigationController.h
// SlideMenu
//
// Created by Aryan Gh on 4/24/13.
// Copyright (c) 2013 Aryan Ghassemi. All rights reserved.
//
// https://github.com/aryaxt/iOS-Slide-Menu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@protocol SlideNavigationControllerDelegate <NSObject>
@optional
- (BOOL)slideNavigationControllerShouldDisplayRightMenu;
- (BOOL)slideNavigationControllerShouldDisplayLeftMenu;
@end

typedef enum{
MenuLeft,
MenuRight,
}Menu;

@interface SlideNavigationController : UINavigationController <UINavigationControllerDelegate>

@property (nonatomic, assign) BOOL avoidSwitchingToSameClassViewController;
@property (nonatomic, assign) BOOL enableSwipeGesture;
@property (nonatomic, strong) UIViewController *righMenu;
@property (nonatomic, strong) UIViewController *leftMenu;
@property (nonatomic, strong) UIBarButtonItem *leftbarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *rightBarButtonItem;

+ (SlideNavigationController *)sharedInstance;
- (void)switchToViewController:(UIViewController *)viewController withCompletion:(void (^)())completion;

@end

######1. SlideNavigationController.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
455
456
457
458
459
460
461
462
463
464
465
466
467
//
// SlideNavigationController.m
// SlideMenu
//
// Created by Aryan Gh on 4/24/13.
// Copyright (c) 2013 Aryan Ghassemi. All rights reserved.
//
// https://github.com/aryaxt/iOS-Slide-Menu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "SlideNavigationController.h"

@interface SlideNavigationController()
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
@property (nonatomic, strong) UIPanGestureRecognizer *panRecognizer;
@property (nonatomic, assign) CGPoint draggingPoint;
@end

@implementation SlideNavigationController
@synthesize righMenu;
@synthesize leftMenu;
@synthesize tapRecognizer;
@synthesize panRecognizer;
@synthesize draggingPoint;
@synthesize leftbarButtonItem;
@synthesize rightBarButtonItem;
@synthesize enableSwipeGesture;

#define MENU_OFFSET 60
#define MENU_SLIDE_ANIMATION_DURATION .3
#define MENU_QUICK_SLIDE_ANIMATION_DURATION .1
#define MENU_IMAGE @"Icon-Menu"//@"menu-button"

static SlideNavigationController *singletonInstance;

#pragma mark - Initialization -

+ (SlideNavigationController *)sharedInstance
{
return singletonInstance;
}

- (void)awakeFromNib
{
[self setup];
}

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
if (self = [super initWithRootViewController:rootViewController])
{
[self setup];
}

return self;
}

- (id)init
{
if (self = [super init])
{
[self setup];
}

return self;
}

- (void)setup
{
self.avoidSwitchingToSameClassViewController = YES;
singletonInstance = self;
self.delegate = self;

self.view.layer.shadowColor = [UIColor darkGrayColor].CGColor;
self.view.layer.shadowRadius = 10;
self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
self.view.layer.shadowOpacity = 1;
self.view.layer.shouldRasterize = YES;
self.view.layer.rasterizationScale = [UIScreen mainScreen].scale;

[self setEnableSwipeGesture:YES];
}

#pragma mark - Public Methods -

- (void)switchToViewController:(UIViewController *)viewController withCompletion:(void (^)())completion
{
if (self.avoidSwitchingToSameClassViewController && [self.topViewController isKindOfClass:viewController.class])
{
[self closeMenuWithCompletion:completion];
return;
}

__block CGRect rect = self.view.frame;

if ([self isMenuOpen])
{
[UIView animateWithDuration:MENU_SLIDE_ANIMATION_DURATION
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
rect.origin.x = (rect.origin.x > 0) ? rect.size.width : -1*rect.size.width;
self.view.frame = rect;
} completion:^(BOOL finished) {

[super popToRootViewControllerAnimated:NO];
[super pushViewController:viewController animated:NO];

[self closeMenuWithCompletion:^{
if (completion)
completion();
}];
}];
}
else
{
[super popToRootViewControllerAnimated:NO];
[super pushViewController:viewController animated:NO];

if (completion)
completion();
}
}

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
{
if ([self isMenuOpen])
{
[self closeMenuWithCompletion:^{
[super popToRootViewControllerAnimated:animated];
}];
}
else
{
return [super popToRootViewControllerAnimated:animated];
}

return nil;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self isMenuOpen])
{
[self closeMenuWithCompletion:^{
[super pushViewController:viewController animated:animated];
}];
}
else
{
[super pushViewController:viewController animated:animated];
}
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self isMenuOpen])
{
[self closeMenuWithCompletion:^{
[super popToViewController:viewController animated:animated];
}];
}
else
{
return [super popToViewController:viewController animated:animated];
}

return nil;
}

#pragma mark - Private Methods -

- (UIBarButtonItem *)barButtonItemForMenu:(Menu)menu
{
SEL selector = (menu == MenuLeft) ? @selector(leftMenuSelected:) : @selector(righttMenuSelected:);
UIBarButtonItem *customButton = (menu == MenuLeft) ? self.leftbarButtonItem : self.rightBarButtonItem;

if (customButton)
{
customButton.action = selector;
customButton.target = self;
return customButton;
}
else
{
UIImage *image = [UIImage imageNamed:MENU_IMAGE];
return [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:selector];
}
}

- (BOOL)isMenuOpen
{
return (self.view.frame.origin.x == 0) ? NO : YES;
}

- (BOOL)shouldDisplayMenu:(Menu)menu forViewController:(UIViewController *)vc
{
if (menu == MenuRight)
{
if ([vc respondsToSelector:@selector(slideNavigationControllerShouldDisplayRightMenu)] &&
[(UIViewController<SlideNavigationControllerDelegate> *)vc slideNavigationControllerShouldDisplayRightMenu])
{
return YES;
}
}
if (menu == MenuLeft)
{
if ([vc respondsToSelector:@selector(slideNavigationControllerShouldDisplayLeftMenu)] &&
[(UIViewController<SlideNavigationControllerDelegate> *)vc slideNavigationControllerShouldDisplayLeftMenu])
{
return YES;
}
}

return NO;
}

- (void)openMenu:(Menu)menu withDuration:(float)duration andCompletion:(void (^)())completion
{
[self.topViewController.view addGestureRecognizer:self.tapRecognizer];

if (menu == MenuLeft)
{
[self.righMenu.view removeFromSuperview];
[self.view.window insertSubview:self.leftMenu.view atIndex:0];
}
else
{
[self.leftMenu.view removeFromSuperview];
[self.view.window insertSubview:self.righMenu.view atIndex:0];
}

[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGRect rect = self.view.frame;
rect.origin.x = (menu == MenuLeft) ? (rect.size.width - MENU_OFFSET) : ((rect.size.width - MENU_OFFSET )* -1);
self.view.frame = rect;
}
completion:^(BOOL finished) {
if (completion)
completion();
}];
}

- (void)openMenu:(Menu)menu withCompletion:(void (^)())completion
{
[self openMenu:menu withDuration:MENU_SLIDE_ANIMATION_DURATION andCompletion:completion];
}

- (void)closeMenuWithDuration:(float)duration andCompletion:(void (^)())completion
{
[self.topViewController.view removeGestureRecognizer:self.tapRecognizer];

[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGRect rect = self.view.frame;
rect.origin.x = 0;
self.view.frame = rect;
}
completion:^(BOOL finished) {
if (completion)
completion();
}];
}

- (void)closeMenuWithCompletion:(void (^)())completion
{
[self closeMenuWithDuration:MENU_SLIDE_ANIMATION_DURATION andCompletion:completion];
}

#pragma mark - UINavigationControllerDelegate Methods -

- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if ([self shouldDisplayMenu:MenuLeft forViewController:viewController])
viewController.navigationItem.leftBarButtonItem = [self barButtonItemForMenu:MenuLeft];

if ([self shouldDisplayMenu:MenuRight forViewController:viewController])
viewController.navigationItem.rightBarButtonItem = [self barButtonItemForMenu:MenuRight];
}

#pragma mark - IBActions -

- (void)leftMenuSelected:(id)sender
{
if ([self isMenuOpen])
[self closeMenuWithCompletion:nil];
else
[self openMenu:MenuLeft withCompletion:nil];

}

- (void)righttMenuSelected:(id)sender
{
if ([self isMenuOpen])
[self closeMenuWithCompletion:nil];
else
[self openMenu:MenuRight withCompletion:nil];
}

#pragma mark - Gesture Recognizing -

- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
[self closeMenuWithCompletion:nil];
}

- (void)panDetected:(UIPanGestureRecognizer *)aPanRecognizer
{
static NSInteger velocityForFollowingDirection = 1000;

CGPoint translation = [aPanRecognizer translationInView:aPanRecognizer.view];
CGPoint velocity = [aPanRecognizer velocityInView:aPanRecognizer.view];

if (aPanRecognizer.state == UIGestureRecognizerStateBegan)
{
self.draggingPoint = translation;
}
else if (aPanRecognizer.state == UIGestureRecognizerStateChanged)
{
NSInteger movement = translation.x - self.draggingPoint.x;
CGRect rect = self.view.frame;
rect.origin.x += movement;

if (rect.origin.x >= self.minXForDragging && rect.origin.x <= self.maxXForDragging)
self.view.frame = rect;

self.draggingPoint = translation;

if (rect.origin.x > 0)
{
[self.righMenu.view removeFromSuperview];
[self.view.window insertSubview:self.leftMenu.view atIndex:0];
}
else
{
[self.leftMenu.view removeFromSuperview];
[self.view.window insertSubview:self.righMenu.view atIndex:0];
}
}
else if (aPanRecognizer.state == UIGestureRecognizerStateEnded)
{
NSInteger currentX = self.view.frame.origin.x;
NSInteger currentXOffset = (currentX > 0) ? currentX : currentX * -1;
NSInteger positiveVelocity = (velocity.x > 0) ? velocity.x : velocity.x * -1;

// If the speed is high enough follow direction
if (positiveVelocity >= velocityForFollowingDirection)
{
// Moving Right
if (velocity.x > 0)
{
if (currentX > 0)
{
[self openMenu:(velocity.x > 0) ? MenuLeft : MenuRight withCompletion:nil];
}
else
{
[self closeMenuWithDuration:MENU_QUICK_SLIDE_ANIMATION_DURATION andCompletion:nil];
}
}
// Moving Left
else
{
if (currentX > 0)
{
[self closeMenuWithCompletion:nil];
}
else
{
Menu menu = (velocity.x > 0) ? MenuLeft : MenuRight;

if ([self shouldDisplayMenu:menu forViewController:self.visibleViewController])
[self openMenu:(velocity.x > 0) ? MenuLeft : MenuRight withDuration:MENU_QUICK_SLIDE_ANIMATION_DURATION andCompletion:nil];
}
}
}
else
{
if (currentXOffset < self.view.frame.size.width/2)
[self closeMenuWithCompletion:nil];
else
[self openMenu:(currentX > 0) ? MenuLeft : MenuRight withCompletion:nil];
}
}
}

- (NSInteger)minXForDragging
{
if ([self shouldDisplayMenu:MenuRight forViewController:self.topViewController])
{
return (self.view.frame.size.width - MENU_OFFSET) * -1;
}

return 0;
}

- (NSInteger)maxXForDragging
{
if ([self shouldDisplayMenu:MenuLeft forViewController:self.topViewController])
{
return self.view.frame.size.width - MENU_OFFSET;
}

return 0;
}

#pragma mark - Setter & Getter -

- (UITapGestureRecognizer *)tapRecognizer
{
if (!tapRecognizer)
{
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
}

return tapRecognizer;
}

- (UIPanGestureRecognizer *)panRecognizer
{
if (!panRecognizer)
{
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
}

return panRecognizer;
}

- (void)setEnableSwipeGesture:(BOOL)markEnableSwipeGesture
{
enableSwipeGesture = markEnableSwipeGesture;

if (enableSwipeGesture)
{
[self.view addGestureRecognizer:self.panRecognizer];
}
else
{
[self.view removeGestureRecognizer:self.panRecognizer];
}
}

@end

效果图

(无)

备注

(无)