自定义UINavigationController开发1

这里写链接内容在实际开发过程中构建我们项目框架时为了用户体验好和个性化往往需要手势返回的功能,虽然ios7以后的系统支持这个但是系统有些不足如:支持手势返回需要ios7.0以上系统,手势非要从左边缘开始拉,触摸拉开效果不能自定义,,然而我们今天就给大家开发一款支持手势返回没有系统要求(ios5.0以上),从哪里可以触摸返回可以自定义的一个导航框架。 我这个是以淘宝app的导航框架为例开发。 WHC_NavigationController.h头文件如下:

: UINavigationController//单例默认用KWHC_Nav_Main_VC_Name作为主控制器+ (instancetype)sharedInstance;//单例自定义主控制器+ (instancetype)sharedInstanceWithMainVC:(UIViewController*)mainVC;@end

WHC_NavigationController.m源文件如下:

()<UINavigationControllerDelegate>{UIPanGestureRecognizer* _panGesture;* _snapshootList;* _pushView;_willOpen;_isTouchPop;//是否触摸弹出}@endstatic WHC_NavigationController * whc_navigation;+ (instancetype)sharedInstance{UIViewController * rootVC = [[NSClassFromString(KWHC_Nav_Main_VC_Name) alloc]init];//UIViewController * rootVC = [[NSClassFromString(KWHC_Side_Menu_N_Main_VC_Name) alloc]initWithNibName:KWHC_Nav_Main_VC_Name bundle:nil];return [WHC_NavigationController sharedInstanceWithMainVC:rootVC];}+ (instancetype)sharedInstanceWithMainVC:(UIViewController*)mainVC{static dispatch_once_t predicate;dispatch_once(&predicate, ^{whc_navigation = [[WHC_NavigationController alloc]initWithRootViewController:mainVC];whc_navigation.delegate = whc_navigation;});return whc_navigation;}#pragma mark – initMothed//重载父类初始化方法- (instancetype)initWithRootViewController:(UIViewController *)rootViewController{self = [super initWithRootViewController:rootViewController];if(self){[self registPanGesture:YES];}return self;}- (instancetype)initWithCoder:(NSCoder *)aDecoder{self = [super initWithCoder:aDecoder];if(self != nil){[self registPanGesture:YES];}return self;}- (instancetype)init{self = [super init];if(self != nil){[self registPanGesture:YES];}return self;}- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if(self){[self registPanGesture:YES];}return self;}#pragma mark – gestureMothed//注册手势事件- (void)registPanGesture:(BOOL)b{= NO;if(b){if(_panGesture == nil){_panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];[_panGesture delaysTouchesBegan];[self.view addGestureRecognizer:_panGesture];}}else{if(_panGesture != nil){[self.view removeGestureRecognizer:_panGesture];_panGesture = nil;}}}#pragma mark – handleGesture- (void)handlePanGesture:(UIPanGestureRecognizer*)panGesture{switch (panGesture.state) {case UIGestureRecognizerStateBegan:{_currentTx = ;}break;case UIGestureRecognizerStateChanged:{CGFloat moveDistance = [panGesture translationInView:panGesture.view].x;if(!_willOpen){if(KWHC_Nav_Pop_Form_Border < (0)){if(moveDistance < KWHC_NAV_ALLOW_PULL_DISTANCE){return;}}else{CGFloat touchX = [panGesture locationInView:panGesture.view].x;if(touchX > KWHC_Nav_Pop_Form_Border){return;}}}CGFloat oriX = [panGesture velocityInView:panGesture.view].x;);if(oriX > 0){//open door[self popView];if(!_willOpen && _popView != nil){viewWithTag:KWHC_NAV_LEFT_VIEW_TAG];if(oldPopView){[oldPopView removeFromSuperview];}[insertSubview:_popView belowSubview:self.view];_willOpen = YES;}if(_willOpen && moveDistance >= 0.0){= [self initAffineTransform:moveDistance + _currentTx];_popView.center = CGPointMake(KWHC_NAV_POP_VIEW_CENTERX_OFFSET + rate * CGRectGetWidth(_popView.frame) / 2.0, _popView.center.y);_popView.alpha = KWHC_NAV_POP_VIEW_ALPHA * (rate + 1.0);}}= [self initAffineTransform:moveDistance + _currentTx];_popView.center = CGPointMake(KWHC_NAV_POP_VIEW_CENTERX_OFFSET + rate * CGRectGetWidth(_popView.frame) / 2.0, _popView.center.y);_popView.alpha = KWHC_NAV_POP_VIEW_ALPHA * (rate + 1.0);}else if(_willOpen){> 0){= [self initAffineTransform:0.0];_popView.center = CGPointMake(KWHC_NAV_POP_VIEW_CENTERX_OFFSET, _popView.center.y);_popView.alpha = KWHC_NAV_POP_VIEW_ALPHA;}}}break;case UIGestureRecognizerStateCancelled:case UIGestureRecognizerStateEnded:{if(_willOpen){/ CGRectGetWidth() < KWHC_Nav_Touch_Back_Rate){[self closeLeftView:YES];}else{[self closeLeftView:NO];}}}break;default:break;}}//偏移变换- (CGAffineTransform)initAffineTransform:(CGFloat)x{return CGAffineTransformMakeTranslation(x, 0.0);}//对当前控制器进行快照- (UIImage*)snapshootNavBar:(UIViewController*)vc{UIGraphicsBeginImageContextWithOptions(, , 0.0);[renderInContext:UIGraphicsGetCurrentContext()];UIImage * snapshootImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return snapshootImage;}//获取要弹出view的快照view- (UIView*)popView{if(_snapshootList != nil && _snapshootList.count > 1){_popView = [_snapshootList lastObject];_popView.center = CGPointMake(KWHC_NAV_POP_VIEW_CENTERX_OFFSET, _popView.center.y);_popView.alpha = KWHC_NAV_POP_VIEW_ALPHA;}else{_popView = nil;}return _popView;}//获取已经push的快照view- (UIView*)pushView{if(_snapshootList != nil && _snapshootList.count > 0){_pushView = [_snapshootList lastObject];_pushView.center = CGPointMake(KWHC_NAV_PUSH_VIEW_CENTERX_OFFSET, _pushView.center.y);_pushView.alpha = 1.0;}else{_pushView = nil;}return _pushView;}//pop或者取消pop操作- (void)closeLeftView:(BOOL)isClose{[self registPanGesture:NO];_willOpen = NO;UIView * mainView = self.view;__weak typeof(self) sf = self;if(isClose){[UIView animateWithDuration:KWHC_NAV_ANIMATION_DURING delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{_popView.center = CGPointMake(KWHC_NAV_POP_VIEW_CENTERX_OFFSET, _popView.center.y);_popView.alpha = KWHC_NAV_POP_VIEW_ALPHA;mainView.transform = [sf initAffineTransform:0.0];} completion:^(BOOL finished) {[sf registPanGesture:YES];[_popView removeFromSuperview];_popView = nil;}];}else{//pop opeartion[UIView animateWithDuration:KWHC_NAV_ANIMATION_DURING delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{mainView.transform = [sf initAffineTransform:CGRectGetWidth(mainView.frame)];_popView.center = CGPointMake(CGRectGetWidth(_popView.frame) / 2.0, _popView.center.y);_popView.alpha = 1.0;} completion:^(BOOL finished) {[sf registPanGesture:YES];_isTouchPop = YES;mainView.transform = [sf initAffineTransform:0];[sf popViewControllerAnimated:NO];}];}}#pragma mark – viewLoad- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}//重载父类push方法- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{if(_snapshootList == nil){_snapshootList = [NSMutableArray array];}[_snapshootList addObject:[[UIImageView alloc]initWithImage:[self snapshootNavBar:viewController]]];[self pushView];//忽略第一次push操作if(_pushView && animated && _snapshootList != nil){__weak typeof(self) sf = self;//防止重复push_topView = ;_topView.userInteractionEnabled = NO;[super pushViewController:viewController animated:NO];= [)];viewWithTag:KWHC_NAV_LEFT_PUSH_VIEW_TAG];if(oldPushView){[oldPushView removeFromSuperview];oldPushView = nil;}_pushView.center = CGPointMake(CGRectGetWidth(_pushView.frame) / 2.0, _pushView.center.y);[insertSubview:_pushView belowSubview:self.view];[UIView animateWithDuration:KWHC_NAV_ANIMATION_DURING delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{_pushView.center = CGPointMake(-CGRectGetWidth(_pushView.frame) / 2.0, _pushView.center.y);_pushView.alpha = KWHC_NAV_POP_VIEW_ALPHA;sf.view.transform = [sf initAffineTransform:0];} completion:^(BOOL finished) {[_pushView removeFromSuperview];_pushView = nil;}];}else{[super pushViewController:viewController animated:animated];}}//重写父类pop方法- (UIViewController *)popViewControllerAnimated:(BOOL)animated{UIViewController * viewController = nil;if(!_isTouchPop){//不是触摸弹出操作__weak typeof(self) sf = self;__block UIView * popView = [[UIImageView alloc]initWithImage:[self snapshootNavBar:nil]];viewController = [super popViewControllerAnimated:NO];if(popView){= [)];= KWHC_NAV_POP_VIEW_ALPHA;viewWithTag:KWHC_NAV_LEFT_VIEW_TAG];if(oldPopView){[oldPopView removeFromSuperview];oldPopView = nil;}popView.center = CGPointMake(CGRectGetWidth(popView.frame) / 2.0, popView.center.y);[insertSubview:popView belowSubview:self.view];[UIView animateWithDuration:KWHC_NAV_ANIMATION_DURING delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{popView.center = CGPointMake(CGRectGetWidth(popView.frame) * 1.5, popView.center.y);sf.view.transform = [sf initAffineTransform:0];sf.view.alpha = 1.0;} completion:^(BOOL finished) {[popView removeFromSuperview];popView = nil;[_snapshootList removeLastObject];}];}}else{//清除popview的快照存储[_snapshootList removeLastObject];[_popView removeFromSuperview];_popView = nil;viewController = [super popViewControllerAnimated:animated];}_isTouchPop = NO;return viewController;}#pragma mark – UINavigationControllerDelegate- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{}- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{if(_topView != nil){_topView.userInteractionEnabled = YES;}}@end

WHC_NavigationControllerDemo下载

每一幢房子都有一种不同的颜色,

自定义UINavigationController开发1

相关文章:

你感兴趣的文章:

标签云: