博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS-navigation中左滑pop的三种方法
阅读量:7114 次
发布时间:2019-06-28

本文共 6822 字,大约阅读时间需要 22 分钟。

iOS-navigation中左滑pop的三种方法

系统自带pop方法

假设我们没有对navigation中的backbutton进行自己定义,我们能够直接使用系统自带的左滑pop方法。可是假设我们对backbutton,进行了自己定义。我们就要对self.navigationController.interactivePopGestureRecognizer这个属性进行设置了。

关键代码

__weak typeof(self) weakSelf = self;
self.navigationController.interactivePopGestureRecognizer.delegate =
weakSelf;

以下是实例代码:

(继承AbeViewController类。就能够使用系统自带的pop方法。)

#import "AbeViewController.h"@interface AbeViewController ()
@end@implementation AbeViewController- (void)viewDidLoad { [super viewDidLoad];}- (void)viewDidAppear:(BOOL)animated{ //**************方法一****************// //设置滑动回退 __weak typeof(self) weakSelf = self; self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf; //推断是否为第一个view if (self.navigationController && [self.navigationController.viewControllers count] == 1) { self.navigationController.interactivePopGestureRecognizer.enabled = NO; }}#pragma mark- UIGestureRecognizerDelegate//**************方法一****************//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ return YES;}@end

自己定义边缘左滑手势方法

就是实现了一个手势方法,触发这个手势方法时pop。

以下是实例代码:

(继承AbeViewController类,就能够使用自己定义边缘左滑手势的pop方法。)

#import "AbeViewController.h"@interface AbeViewController ()
@end@implementation AbeViewController- (void)viewDidLoad { [super viewDidLoad];}- (void)viewDidAppear:(BOOL)animated{ //*************方法二*****************// UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)]; edgePanGestureRecognizer.delegate = self; edgePanGestureRecognizer.edges = UIRectEdgeLeft; [self.view addGestureRecognizer:edgePanGestureRecognizer];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];}#pragma mark- private method//*************方法二*****************//- (void)edgePanGesture:(UIScreenEdgePanGestureRecognizer*)edgePanGestureRecognizer{ [self.navigationController popViewControllerAnimated:YES];}#pragma mark- UIGestureRecognizerDelegate//**************方法二****************//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ if (self.navigationController && [self.navigationController.viewControllers count] == 1) { return NO; } return YES;}@end

自己定义view不论什么位置左移pop

在view中,不论什么位置左移触发pop方法。

事实上就是建立了一个UIPanGestureRecognizer手势。然后该手势触发方法。

知识点:

[panGestureRecognizer locationInView:XX] 获取pan手势的CGPoint。

panGestureRecognizer.state pan的状态
self.interactivePopGestureRecognizer.enabled = NO; 原生左滑无效

以下是实例代码:

(继承ABENavViewController类。就能够使用自己定义view左滑手势的pop方法。 ABENavViewController为UINavigationController的子类)

#import "ABENavViewController.h"@interface ABENavViewController ()@property (strong, nonatomic)UIPanGestureRecognizer *panGestureRecognizer;@property (strong, nonatomic)UIImageView *backView;@property (strong, nonatomic)NSMutableArray *backImgs;@property (assign) CGPoint panBeginPoint;@property (assign) CGPoint panEndPoint;@end@implementation ABENavViewController- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        //initlization    }    return self;}- (void)loadView{    [super loadView];    [self initilization];}- (void)viewDidLoad {    [super viewDidLoad];    [self loadBaseUI];}- (void)initilization{    self.backImgs = [[NSMutableArray alloc] init];}- (void)loadBaseUI{    //原生方法无效    self.interactivePopGestureRecognizer.enabled = NO;    //设置手势    self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];    [self.view addGestureRecognizer:self.panGestureRecognizer];}#pragma mark- public method- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{    //截图    UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, YES, 1.0);    [[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    [self.backImgs addObject:img];    [super pushViewController:viewController animated:animated];}- (UIViewController *)popViewControllerAnimated:(BOOL)animated{    [_backImgs removeLastObject];    return [super popViewControllerAnimated:animated];}#pragma mark- private method- (void)panGestureRecognizerAction:(UIPanGestureRecognizer*)panGestureRecognizer{    if ([self.viewControllers count] == 1) {        return ;    }    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {        NSLog(@"滑动開始");        //存放滑动開始的位置        self.panBeginPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow];        //插入图片        [self insertLastViewFromSuperView:self.view.superview];    }else if(panGestureRecognizer.state == UIGestureRecognizerStateEnded){        NSLog(@"滑动结束");        //存放数据        self.panEndPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow];        if ((_panEndPoint.x - _panBeginPoint.x) > 50) {            [UIView animateWithDuration:0.3 animations:^{                [self moveNavigationViewWithLenght:[UIScreen mainScreen].bounds.size.width];            } completion:^(BOOL finished) {                [self removeLastViewFromSuperView];                [self moveNavigationViewWithLenght:0];                [self popViewControllerAnimated:NO];            }];        }else{            [UIView animateWithDuration:0.3 animations:^{                [self moveNavigationViewWithLenght:0];            }];        }    }else{        //加入移动效果        CGFloat panLength = ([panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow].x - _panBeginPoint.x);        if (panLength > 0) {            [self moveNavigationViewWithLenght:panLength];        }    }}/** *  移动视图界面 * *  @param lenght 移动的长度 */- (void)moveNavigationViewWithLenght:(CGFloat)lenght{    //图片位置设置    self.view.frame = CGRectMake(lenght, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);    //图片动态阴影    _backView.alpha = (lenght/[UIScreen mainScreen].bounds.size.width)*2/3 + 0.33;}/** *  插图上一级图片 * *  @param superView 图片的superView */- (void)insertLastViewFromSuperView:(UIView *)superView{    //插入上一级视图背景    if (_backView == nil) {        _backView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];        _backView.image = [_backImgs lastObject];;    }    [self.view.superview insertSubview:_backView belowSubview:self.view];}/** *  移除上一级图片 */- (void)removeLastViewFromSuperView{    [_backView removeFromSuperview];    _backView = nil;}@end

转载于:https://www.cnblogs.com/clnchanpin/p/7230398.html

你可能感兴趣的文章
Java Scoket之java.io.EOFException解决方案
查看>>
Oracle分析函数四——函数RANK,DENSE_RANK,FIRST,LAST…
查看>>
Android应用程序启动过程源代码分析(5)
查看>>
Linux桌面版横评:八、中标普华桌面Linux 3.0.1
查看>>
让UpdatePanel支持文件上传(5):支持页面重定向的HttpModule
查看>>
Java线程:新特征-阻塞栈
查看>>
CentOS 5.5升级内核到2.6.35.4
查看>>
查询整个数据库中某个特定值所在的表和字段的方法
查看>>
JS把数组中相同元素组合成一个新的数组问题
查看>>
《Linux内核设计与实现》读书笔记(三)- Linux的进程
查看>>
AngularJS快速入门指南08:表格
查看>>
C++矩阵运算库推荐
查看>>
在存储过程中编写正确的事务处理代码(SQL Server 2000 & 2005)
查看>>
Android 控件在布局中按比例放置[转]
查看>>
内核通知链 学习笔记 【转】
查看>>
Input Method of Win32 System
查看>>
count(*) VS count(X)
查看>>
MS ASP.Net Ajax 服务端扩展
查看>>
android102 查询,插入联系人
查看>>
数据库邮件
查看>>