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

在iOS 7还没有发布的时候,各种App实现各种的swipe to pop效果,比如。

在iOS 7上,只要是符合系统的导航结构:[即根视图由导航控制器控制]

//  Created by wangyuanyuan on 14-9-15.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    FirstViewController * firstVC = [[FirstViewController alloc]init];
    SecondViewController * secondVC = [[SecondViewController alloc]init];
    
    NSArray * arr = [NSArray arrayWithObjects:firstVC,secondVC, nil];
    
    UITabBarController * tabVC = [[UITabBarController alloc]init];
    tabVC.viewControllers = arr;
    
    UINavigationController * mainVC = [[UINavigationController alloc]initWithRootViewController:tabVC];
    self.window.rootViewController = mainVC;
    
    [self.window makeKeyAndVisible];
    return YES;
}

 就能够有原来漂亮的效果:

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

 1 - (void)viewWillAppear:(BOOL)animated
 2 {
 3     if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
 4         UIScreenEdgePanGestureRecognizer *left2rightSwipe = [[UIScreenEdgePanGestureRecognizer alloc]
 5                                                               initWithTarget:self
 6                                                               action:@selector(handleSwipeGesture:)];
 7         [left2rightSwipe setDelegate:self];
 8         [left2rightSwipe setEdges:UIRectEdgeLeft];
 9         [self.view addGestureRecognizer:left2rightSwipe];
10     }
11 
12 }
13 
14 
15 - (void)handleSwipeGesture:(UIScreenEdgePanGestureRecognizer *)gestureRecognizer
16 {
17     [self.navigationController popViewControllerAnimated:YES];
18 }

这也实现了相关的返回功能的效果,具体还有相关的手势相关的干扰问题,详情请见博客:http://blog.csdn.net/jasonblog/article/details/16867587

原文地址:https://www.cnblogs.com/yuanyuandachao/p/3974760.html