Objective-C iOS Developer

iOS 记录navigationController的全屏滑动

2015-12-30  本文已影响1344人  劉光軍_MVP

看到@J_雨大牛的博客,讲的很细致,在此记录下第二种方法,以备以后使用,原文链接:http://www.jianshu.com/p/d39f7d22db6c

#import "Nav.h"

@interface Nav ()<UIGestureRecognizerDelegate>

@end

@implementation Nav

- (void)viewDidLoad {
    [super viewDidLoad];
    
          //拿到navigationController原有的pop手势
    UIGestureRecognizer *gesture = self.interactivePopGestureRecognizer;
        //将自有手势停用
    gesture.enabled = NO;
        //拿到手势的view
    UIView *gestureView = gesture.view;
    
        //创建一个自己的手势
    UIPanGestureRecognizer *popRecognizer = [[UIPanGestureRecognizer alloc] init];
    popRecognizer.delegate = self;
    popRecognizer.maximumNumberOfTouches = 1;
      //将自己创建的手势放到原有手势的view上
    [gestureView addGestureRecognizer:popRecognizer];
    
        //获取系统的手势的target数组
    NSMutableArray *_targets = [gesture valueForKey:@"_targets"];
        //获取它的唯一对象 有一个叫UIGestureRecognizerTarget的私有类,有一个属性叫_target
    id gestureRecognizerTarget = [_targets firstObject];
        //获取_target:_UINavigationInteractiveTransition 有一个方法叫handleNavigationTranstion
    id navigationInteractiveTransition = [gestureRecognizerTarget valueForKey:@"_target"];
        //从控制台获取出他的方法签名
    SEL handelTransition = NSSelectorFromString(@"handleNavigationTransition:");
        //创建一个与系统一毛一样的手势 我们只把他的类改为UIPanGestureRecognizer
    [popRecognizer addTarget:navigationInteractiveTransition action:handelTransition];

}

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

使用时

#import "AppDelegate.h"
#import "Nav.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    ViewController *vc = [[ViewController alloc] init];
    Nav *nav = [[Nav alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
    return YES;
}
上一篇下一篇

猜你喜欢

热点阅读