• 【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之间到底是怎样的一个影响关系呢? 
  • 相关阅读:
    react redux
    react 路由 react-router@3.2.1
    react 生命周期
    重载 UINavigationController 设置左侧返回按钮的文字为图片
    使用 SQLiteManager 操作 sqlite3 数据库
    使用 DES 算法对数据加密
    使用 Reachability 获取网络状态
    NSPredicate 的使用(持续更新)
    使用开源库 SDWebImage 异步下载缓存图片(持续更新)
    Grand Central Dispatch (GCD)
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3437023.html
Copyright © 2020-2023  润新知