├── .gitignore ├── KKNavigationController ├── KKNavigationController.h └── KKNavigationController.m ├── LICENSE ├── README.md └── SHOW_GIF └── 18.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | .DS_Store 3 | */build/* 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | profile 14 | *.moved-aside 15 | DerivedData 16 | .idea/ 17 | *.hmap 18 | *.xccheckout 19 | 20 | #CocoaPods 21 | Pods 22 | -------------------------------------------------------------------------------- /KKNavigationController/KKNavigationController.h: -------------------------------------------------------------------------------- 1 | // 2 | // KKNavigationController.h 3 | // TS 4 | // 5 | // Created by Coneboy_K on 13-12-2. 6 | // Copyright (c) 2013年 Coneboy_K. All rights reserved. MIT 7 | // WELCOME TO MY BLOG http://www.coneboy.com 8 | // 9 | 10 | #import 11 | 12 | #define KEY_WINDOW [[UIApplication sharedApplication]keyWindow] 13 | #define kkBackViewHeight [UIScreen mainScreen].bounds.size.height 14 | #define kkBackViewWidth [UIScreen mainScreen].bounds.size.width 15 | 16 | #define iOS7 ( [[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending ) 17 | 18 | // 背景视图起始frame.x 19 | #define startX -200; 20 | 21 | 22 | @interface KKNavigationController : UINavigationController 23 | { 24 | CGFloat startBackViewX; 25 | } 26 | 27 | // 默认为特效开启 28 | @property (nonatomic, assign) BOOL canDragBack; 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /KKNavigationController/KKNavigationController.m: -------------------------------------------------------------------------------- 1 | // 2 | // KKNavigationController.m 3 | // TS 4 | // 5 | // Created by Coneboy_K on 13-12-2. 6 | // Copyright (c) 2013年 Coneboy_K. All rights reserved. MIT 7 | // WELCOME TO MY BLOG http://www.coneboy.com 8 | // 9 | 10 | #import "KKNavigationController.h" 11 | #import 12 | #import 13 | 14 | @interface KKNavigationController () 15 | { 16 | CGPoint startTouch; 17 | 18 | UIImageView *lastScreenShotView; 19 | UIView *blackMask; 20 | 21 | } 22 | 23 | @property (nonatomic,retain) UIView *backgroundView; 24 | @property (nonatomic,retain) NSMutableArray *screenShotsList; 25 | 26 | @property (nonatomic,assign) BOOL isMoving; 27 | 28 | @end 29 | 30 | @implementation KKNavigationController 31 | 32 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 33 | { 34 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 35 | if (self) { 36 | 37 | self.screenShotsList = [[NSMutableArray alloc]initWithCapacity:2]; 38 | self.canDragBack = YES; 39 | 40 | } 41 | return self; 42 | } 43 | 44 | - (void)dealloc 45 | { 46 | self.screenShotsList = nil; 47 | 48 | [self.backgroundView removeFromSuperview]; 49 | self.backgroundView = nil; 50 | 51 | } 52 | 53 | - (void)viewDidLoad 54 | { 55 | [super viewDidLoad]; 56 | 57 | UIImageView *shadowImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"leftside_shadow_bg"]]; 58 | shadowImageView.frame = CGRectMake(-10, 0, 10, self.view.frame.size.height); 59 | [self.view addSubview:shadowImageView]; 60 | 61 | UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self 62 | action:@selector(paningGestureReceive:)]; 63 | [recognizer setDelegate:self]; 64 | [recognizer delaysTouchesBegan]; 65 | [self.view addGestureRecognizer:recognizer]; 66 | } 67 | 68 | - (void)didReceiveMemoryWarning 69 | { 70 | [super didReceiveMemoryWarning]; 71 | // Dispose of any resources that can be recreated. 72 | } 73 | 74 | - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated 75 | { 76 | [self.screenShotsList addObject:[self capture]]; 77 | 78 | [super pushViewController:viewController animated:animated]; 79 | } 80 | 81 | - (UIViewController *)popViewControllerAnimated:(BOOL)animated 82 | { 83 | [self.screenShotsList removeLastObject]; 84 | 85 | return [super popViewControllerAnimated:animated]; 86 | } 87 | 88 | - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{ 89 | NSInteger index = [self.viewControllers indexOfObject:viewController]; 90 | if (index != NSNotFound) { 91 | [self.screenShotsList removeObjectsInRange:NSMakeRange(index + 1, self.viewControllers.count - index - 1)]; 92 | } 93 | return [super popToViewController:viewController animated:animated]; 94 | } 95 | 96 | #pragma mark - Utility Methods - 97 | 98 | - (UIImage *)capture 99 | { 100 | UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0); 101 | [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 102 | 103 | UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 104 | 105 | UIGraphicsEndImageContext(); 106 | 107 | return img; 108 | } 109 | 110 | - (void)moveViewWithX:(float)x 111 | { 112 | x = x>self.view.bounds.size.width?self.view.bounds.size.width:x; 113 | x = x<0?0:x; 114 | 115 | CGRect frame = self.view.frame; 116 | frame.origin.x = x; 117 | self.view.frame = frame; 118 | 119 | float alpha = 0.4 - (x/800); 120 | 121 | blackMask.alpha = alpha; 122 | 123 | CGFloat aa = abs(startBackViewX)/kkBackViewWidth; 124 | CGFloat y = x*aa; 125 | 126 | UIImage *lastScreenShot = [self.screenShotsList lastObject]; 127 | CGFloat lastScreenShotViewHeight = lastScreenShot.size.height; 128 | CGFloat superviewHeight = lastScreenShotView.superview.frame.size.height; 129 | CGFloat verticalPos = superviewHeight - lastScreenShotViewHeight; 130 | 131 | [lastScreenShotView setFrame:CGRectMake(startBackViewX+y, 132 | verticalPos, 133 | kkBackViewWidth, 134 | lastScreenShotViewHeight)]; 135 | } 136 | 137 | 138 | 139 | -(BOOL)isBlurryImg:(CGFloat)tmp 140 | { 141 | return YES; 142 | } 143 | 144 | #pragma mark - Gesture Recognizer - 145 | //不响应的手势则传递下去 146 | - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 147 | { 148 | if (self.viewControllers.count <= 1 || !self.canDragBack){ 149 | return NO; 150 | } 151 | return YES; 152 | } 153 | 154 | 155 | - (void)paningGestureReceive:(UIPanGestureRecognizer *)recoginzer 156 | { 157 | if (self.viewControllers.count <= 1 || !self.canDragBack) return; 158 | 159 | CGPoint touchPoint = [recoginzer locationInView:KEY_WINDOW]; 160 | 161 | if (recoginzer.state == UIGestureRecognizerStateBegan) { 162 | 163 | _isMoving = YES; 164 | startTouch = touchPoint; 165 | 166 | if (!self.backgroundView) 167 | { 168 | CGRect frame = self.view.frame; 169 | 170 | self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width , frame.size.height)]; 171 | [self.view.superview insertSubview:self.backgroundView belowSubview:self.view]; 172 | 173 | blackMask = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width , frame.size.height)]; 174 | blackMask.backgroundColor = [UIColor blackColor]; 175 | [self.backgroundView addSubview:blackMask]; 176 | } 177 | 178 | self.backgroundView.hidden = NO; 179 | 180 | if (lastScreenShotView) [lastScreenShotView removeFromSuperview]; 181 | 182 | 183 | UIImage *lastScreenShot = [self.screenShotsList lastObject]; 184 | 185 | lastScreenShotView = [[UIImageView alloc]initWithImage:lastScreenShot]; 186 | 187 | startBackViewX = startX; 188 | [lastScreenShotView setFrame:CGRectMake(startBackViewX, 189 | lastScreenShotView.frame.origin.y, 190 | lastScreenShotView.frame.size.height, 191 | lastScreenShotView.frame.size.width)]; 192 | 193 | [self.backgroundView insertSubview:lastScreenShotView belowSubview:blackMask]; 194 | 195 | }else if (recoginzer.state == UIGestureRecognizerStateEnded || recoginzer.state == UIGestureRecognizerStateCancelled){ 196 | [self _panGestureRecognizerDidFinish:recoginzer]; 197 | } else if (recoginzer.state == UIGestureRecognizerStateChanged) { 198 | 199 | if (_isMoving) { 200 | [self moveViewWithX:touchPoint.x - startTouch.x]; 201 | } 202 | } 203 | 204 | } 205 | 206 | // 当手势结束的时候,会根据当前滑动的速度,以及当前的位置综合去计算将要移动到的位置。这样用户操作起来感觉比较自然,不然产生的情况就是,假设我移动的速度很快,但是停下来的时候没有滑动到某个位置,导致我还是不能返回。这个可以参考iOS7原生UINavigationController的效果,或者参考STKit中提供的STNavigationController的效果 207 | //https://github.com/lovesunstar/stkitdemo/blob/master/STKitDemo/Classes/STKit.framework/Headers/STNavigationController.h 208 | - (void)_panGestureRecognizerDidFinish:(UIPanGestureRecognizer *)panGestureRecognizer { 209 | // 这里应该用navigationController.view.width, 这里由于整个项目都是这样,就不做改动了 210 | CGFloat navigationWidth = CGRectGetWidth(KEY_WINDOW.bounds); 211 | 212 | // 获取手指离开时候的速度 213 | CGFloat velocityX = [panGestureRecognizer velocityInView:KEY_WINDOW].x; 214 | CGPoint translation = [panGestureRecognizer translationInView:KEY_WINDOW]; 215 | // 这里的targetX是根据scrollView松手时候猜想的,这个可以动态的稍微调整一下。 216 | CGFloat tempTargetX = MIN(MAX(translation.x + (velocityX * 0.2), 0), navigationWidth); 217 | CGFloat gestureTargetX = (tempTargetX + translation.x) / 2; 218 | // 当前push/pop完成的百分比,根据这个百分比,可以计算得到剩余动画的时间。像现在存在一个BUG,比如我稍微移动了一下,然后返回,这个时候也是按照0.3s就不太合适 219 | CGFloat completionPercent = gestureTargetX / navigationWidth; 220 | CGFloat moveTargetX = 0; 221 | CGFloat duration; 222 | BOOL finishPop = NO; 223 | if (gestureTargetX > navigationWidth * 0.6) { 224 | // 需要pop, pop的总时间是0.3, 完成了percent,还剩余1-percent 225 | duration = 0.3 * (1.0 - completionPercent); 226 | moveTargetX = navigationWidth; 227 | finishPop = YES; 228 | } else { 229 | // 再push回去,如果已经pop了百分之percent,则时间就是completionPercent *0.3 230 | duration = completionPercent * 0.3; 231 | } 232 | duration = MAX(MIN(duration, 0.3), 0.01); 233 | void (^completion)(BOOL) = ^(BOOL finished) { 234 | _isMoving = NO; 235 | if (finishPop) { 236 | [self popViewControllerAnimated:NO]; 237 | CGRect frame = self.view.frame; 238 | frame.origin.x = 0; 239 | self.view.frame = frame; 240 | } else { 241 | self.backgroundView.hidden = YES; 242 | } 243 | }; 244 | [UIView animateWithDuration:duration animations:^{ 245 | [self moveViewWithX:finishPop ? navigationWidth : 0]; 246 | } completion:completion]; 247 | } 248 | 249 | @end 250 | 251 | 252 | 253 | 254 | 255 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Coneboy_k 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | KKNavigationController 2 | ====================== 3 | 4 | 5 | This is a NavigationController about Parallax,it like 网易app 6 | 7 | [![](https://github.com/Coneboy-k/KKNavigationController/blob/master/SHOW_GIF/18.gif?raw=true)] 8 | 9 | ## Requirements 10 | KKNavigationController works on any iOS version. Only ARC. 11 | 12 | ## Adding KKNavigationController to your project 13 | 14 | Include KKNavigationController wherever you need it with `#import "KKNavigationController.h"`. 15 | 16 | ### Source files 17 | 18 | Alternatively you can directly add the `KKNavigationController.h`& `KKNavigationController.m` source files to your project. 19 | 20 | 1. Download the [latest code version](https://github.com/Coneboy-k/KKNavigationController/archive/master.zip) or add the repository as a git submodule to your git-tracked project. 21 | 2. Open your project in Xcode, then drag and drop `KKNavigationController.h`& `KKNavigationController.m` onto your project (use the "Product Navigator view"). Make sure to select Copy items when asked if you extracted the code archive outside of your project. 22 | 3. Include KKNavigationController wherever you need it with `#import "KKNavigationController.h"`. 23 | 24 | ## Usage 25 | 26 | self.viewController = [[KKNavigationController alloc] 27 | initWithRootViewController: 28 | [[TSViewController alloc]init]]; 29 | 30 | 31 | ## Who use 32 | 33 | >图说 http://www.tushuoapp.com/ 34 | 35 | 36 | ## License 37 | 38 | This code is distributed under the terms and conditions of the [MIT license](LICENSE). 39 | 40 | ## Change-log 41 | 42 | A brief summary of each KKNavigationController release can be found on the [wiki](https://github.com/Coneboy-k/KKNavigationController/wiki). 43 | -------------------------------------------------------------------------------- /SHOW_GIF/18.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Coneboy-k/KKNavigationController/11356916b26ca1e50882f1f3a215b0fe60f41471/SHOW_GIF/18.gif --------------------------------------------------------------------------------