-
Notifications
You must be signed in to change notification settings - Fork 0
/
UINavigationController+Slider.m
executable file
·117 lines (93 loc) · 3.89 KB
/
UINavigationController+Slider.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
//
// UINavigationController+Slider.m
// Slider_Testing
//
// Created by zhulaifei on 16/11/30.
// Copyright © 2016年 WanCun. All rights reserved.
//
#import "UINavigationController+Slider.h"
#import <objc/runtime.h>
static NSString * const UINavigationKey = @"UINavigationKey" ;
static NSString * const UINavigationBarItemKey = @"UINavigationBarItemKey" ;
@interface UINavigationController()<UINavigationControllerDelegate>
@end
@implementation UINavigationController (Slider)
-(UIViewController *)leftMenu{
return objc_getAssociatedObject(self, &UINavigationKey) ;
}
-(void)setLeftMenu:(UIViewController *)leftMenu{
objc_setAssociatedObject(self, &UINavigationKey, leftMenu, OBJC_ASSOCIATION_RETAIN) ;
}
-(UIBarButtonItem *)leftbarButtonItem{
return objc_getAssociatedObject(self, &UINavigationBarItemKey) ;
}
-(void)setLeftbarButtonItem:(UIBarButtonItem *)leftbarButtonItem{
objc_setAssociatedObject(self, &UINavigationBarItemKey, leftbarButtonItem, OBJC_ASSOCIATION_RETAIN) ;
}
-(void)viewDidLoad{
[super viewDidLoad] ;
[self setup] ;
}
-(void)setup{
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;
}
#pragma mark - UINavigationControllerDelegate Methods -
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
Class cls = NSClassFromString(@"ViewController1") ;
if ([viewController isKindOfClass:[cls class]]) {
UIImage * image = [UIImage imageNamed:@"menu-button"];
UIBarButtonItem *leftBarbuttonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftMenuClick:)];
viewController.navigationItem.leftBarButtonItem = leftBarbuttonItem ;
}
}
-(void)leftMenuClick :(UIBarButtonItem *)item{
if ([self isMenuOpen] == NO) {
[self closeMenuWithCompletionBlock:nil] ;
}else{
[self openMenuWthComPletionBlock:nil] ;
}
}
-(void)closeMenuWithCompletionBlock: (void(^)())completion{
[self closeMenuWithDuration:0.2 andCompletion:completion] ;
}
-(void)closeMenuWithDuration:(CGFloat)duration andCompletion:(void(^)())completion{
[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)openMenuWthComPletionBlock:(void(^)())completion{
[self openMenuWithDuration:0.2 andCompletion:completion] ;
}
-(void)openMenuWithDuration:(CGFloat)duration andCompletion:(void(^)())completion{
[self.view.window insertSubview:self.leftMenu.view atIndex:0];
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGRect rect = self.view.frame ;
rect.origin.x =rect.size.width / 2 ;
self.view.frame = rect;
} completion:^(BOOL finished) {
if (completion) {
completion() ;
}
}];
}
-(BOOL)isMenuOpen{
return self.view.frame.origin.x == 0;
}
@end