【iOS 7】使用UIScreenEdgePanGestureRecognizer实现swipe to pop效果

在iOS 7还没有发布的时候,各种App实现各种的swipe to pop效果,比如这里有一份简单的demo

在iOS 7上,只要是符合系统的导航结构:

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.navController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
    self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];
    return YES;
}


就能够拥有原生的漂亮效果:

 


遗憾的是目前项目代码里没有遵循iOS系统的导航结构,使得无法利用这点好处。所以我考虑使用新增的UIScreenEdgePanGestureRecognizer手势:

 

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        UIScreenEdgePanGestureRecognizer *left2rightSwipe = [[[UIScreenEdgePanGestureRecognizer alloc]
                                                              initWithTarget:self
                                                              action:@selector(handleSwipeGesture:)]
                                                             autorelease];
        [left2rightSwipe setDelegate:self];
        [left2rightSwipe setEdges:UIRectEdgeLeft];
        [self.view addGestureRecognizer:left2rightSwipe];
    }
}

- (void)handleSwipeGesture:(UIScreenEdgePanGestureRecognizer *)gestureRecognizer
{
    [self.navigationController popViewControllerAnimated:YES];
}


刚加上去的时候各种无法触发,很郁闷——毕竟是在既有代码添加,而不是在一个干净的工程上。

 

后来发现现有代码实现了是否开始解释touch的代理函数,稍作修改得以生效:

 

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        if ([gestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
            return YES;
        }
    }


生效了以后但是体验不好,感觉不灵敏。原来是已经添加过tapGesture,受了影响,又做了进一步修改:

 

 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    BOOL result = NO;
    
    if ([gestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
        result = YES;
    }
    
    return result;
}


如此一番才觉得尚可。

 

上面这个代理函数的描述如下:

 

Asks the delegate if a gesture recognizer should be required to fail by another gesture recognizer.


 

 

gestureRecognizer和otherGestureRecognizer之间到底是怎样的一个影响关系呢? 

 

posted on 2013-11-22 10:28  我的小人生  阅读(497)  评论(0编辑  收藏  举报