UINavigationController添加页面滑动跳转 objective-c
2016-03-11 09:57
525 查看
方式一:
.h#import <UIKit/UIKit.h>
@interface MTMainNavigationC : UINavigationController
@end
.m
方式二:
.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
.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
相关文章推荐
- Objective-C之Category
- Objective-C中的Block
- Objective-C 中解决 Byte 转换成 NSString 出现的 NULL
- 详解Javascript中的Object对象
- WaitForSingleObject的用法
- Python字符串与数字拼接 TypeError: cannot concatenate 'str' and 'int' objects
- Objective-C的可变是如何实现的?
- Objective-C之用C的字符来处理NSString相关的字符替换和拼接的问题
- IOS-Swift、Objective-C、C++混合编程
- Objective-C之@类别小实例
- Objective-C之@class的使用
- Objective-C Reflection(Objective-C 反射机制)实用随笔笔记(持续更新)
- Objective-C之Block
- Swift语言与Objective-C语言混合编程
- Objective-C中常用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect
- Objective-C之代理设计模式小实例
- Objective-C的可变是如何实现的?
- Objective-C之用C的字符来处理NSString相关的字符替换和拼接的问题
- IOS-Swift、Objective-C、C++混合编程
- Objective-C之@类别小实例