UINavigationController添加页面滑动跳转 objective-c

方式一:

.h

#import <UIKit/UIKit.h>

@interface MTMainNavigationC : UINavigationController

@end
.m
//
//  MTMainNavigationC.m
//  MTDemo_date
//
//  Created by apple on 15/12/30.
//  Copyright © 2015年 Bocheng network technology co., LTD. All rights reserved.
//

#import "MTMainNavigationC.h"

@interface MTMainNavigationC ()<UINavigationControllerDelegate>


@end

@implementation MTMainNavigationC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.delegate = self;
    

}

// 导航控制器加载完毕一个View的时候就会调用这个方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == self.childViewControllers[0])
    {
        [self addGesture:nil];
    }else{
        [self addGesture:@selector(handleNavigationTransition:)];
    }
}



- (void)addGesture:(SEL)action
{
    UIScreenEdgePanGestureRecognizer *gesture = (UIScreenEdgePanGestureRecognizer *)self.interactivePopGestureRecognizer;
    gesture.enabled  = NO;
    NSMutableArray *targets = [gesture valueForKeyPath:@"_targets"];
    id gestureTarget = [targets firstObject];
    id target = [gestureTarget valueForKeyPath:@"_target"];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:action];
    [gesture.view addGestureRecognizer:pan];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

方式二:

.h

//
//  MTMainNavigationTwoC.h
//  MTDemo_date
//
//  Created by apple on 15/12/30.
//  Copyright © 2015年 Bocheng network technology co., LTD. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MTMainNavigationTwoC : UINavigationController

@end
.m
//
//  MTMainNavigationTwoC.m
//  MTDemo_date
//
//  Created by apple on 15/12/30.
//  Copyright © 2015年 Bocheng network technology co., LTD. All rights reserved.
//

#import "MTMainNavigationTwoC.h"

@interface MTMainNavigationTwoC ()<UIGestureRecognizerDelegate>

@end

@implementation MTMainNavigationTwoC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 获取屏幕边缘手势识别器
    UIScreenEdgePanGestureRecognizer *gesture = (UIScreenEdgePanGestureRecognizer *)self.interactivePopGestureRecognizer;
    
    // 先禁止系统的左侧滑手势
    gesture.enabled  = NO;
    
    // 利用KVC强制获取手势数组
    // 利用运行时查看私有属性
    NSMutableArray *targets = [gesture valueForKeyPath:@"_targets"];
    
    // 获取这个这个手势对象
    id gestureTarget = [targets firstObject];
    
    // 利用KVC获取手势的私有属性target
    id target = [gestureTarget valueForKeyPath:@"_target"];
    
    // 创建滑动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    
    // 设置自定义的手势代理为控制器,这样自定义的手势就会调用代理的这个方法`gestureRecognizerShouldBegin:`
    pan.delegate = self;
    
    // 添加自定义手势到手势识别器的View中
    [gesture.view addGestureRecognizer:pan];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    // 这里有两个条件不允许手势执行:1、当前控制器为根控制器;2、如果这个push、pop动画正在执行(私有属性)
    return self.viewControllers.count != 1 && ![[self valueForKey:@"_isTransitioning"] boolValue];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值