欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS滑动全屏实现返回功能

时间:2021-05-08 09:05:10|栏目:iOS代码|点击:

本文实例为大家分享了iOS滑动全屏实现返回功能的具体代码,供大家参考,具体内容如下

系统自带的滑动返回功能,只能滑动边缘返回,而我们希望通过滑动全屏实现返回功能。

定义BaseNavigationController来取代UINavigationController

// BaseNavigationController.h
#import <UIKit/UIKit.h>

@interface BaseNavigationController : UINavigationController

@end

// BaseNavigationController.m
#import "BaseNavigationController.h"

@interface BaseNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation BaseNavigationController

#pragma mark - 系统方法
+ (void)load
{
 UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];

 // 只要是通过模型设置,都是通过富文本设置
 // 设置导航条标题 => UINavigationBar
 NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
 attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20.0];
 [navBar setTitleTextAttributes:attrs];

 // 设置导航条背景图片
 [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.

 // 自己控制返回手势
// self.interactivePopGestureRecognizer.delegate = self;
// NSLog(@"self.interactivePopGestureRecognizer : %@",self.interactivePopGestureRecognizer);
// NSLog(@"self.interactivePopGestureRecognizer.delegate : %@",self.interactivePopGestureRecognizer.delegate);

 // 全屏返回手势,而不是边缘返回手势
 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
 [self.view addGestureRecognizer:pan];
 // 控制手势什么时候触发,只有非根控制器才需要出发手势
 pan.delegate = self;
 // 禁止之前手势
 self.interactivePopGestureRecognizer.enabled = NO;
}

/**
 * self.interactivePopGestureRecognizer : <UIScreenEdgePanGestureRecognizer: 0x7fb57dc23510; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fb57dc220e0>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fb57dc1c570>)>>
 * self.interactivePopGestureRecognizer.delegate : <_UINavigationInteractiveTransition: 0x7fb57dc1c570>
 */

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
 // 当不是根控制器时才会触发返回手势
 return (self.childViewControllers.count > 1);
}

#pragma mark - 重写
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
 if (self.childViewControllers.count > 0) { // 代表不是根控制器

  // 自定义返回按钮覆盖了系统的返回手势
  viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem setBackButtonItemWithImage:[UIImage imageNamed:@"navigationButtonReturn"] highlightedImage:[UIImage imageNamed:@"navigationButtonReturnClick"] target:self action:@selector(backClick) title:@"返回"];
 }

 [super pushViewController:viewController animated:animated];
}

- (void)backClick
{
 [self popViewControllerAnimated:YES];
}

@end

UIBarButtonItem+item

// UIBarButtonItem+item.h
#import <UIKit/UIKit.h>

@interface UIBarButtonItem (item)

+ (UIBarButtonItem *)setBarButtonItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action;

+ (UIBarButtonItem *)setBarButtonItemWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage target:(id)target action:(SEL)action;

+ (UIBarButtonItem *)setBackButtonItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action title:(NSString *)title;

@end


// UIBarButtonItem+item.m
#import "UIBarButtonItem+item.h"

@implementation UIBarButtonItem (item)

+ (UIBarButtonItem *)setBarButtonItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action
{
 // 1.leftBarButtonItem
 UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [leftButton setImage:image forState:UIControlStateNormal];
 [leftButton setImage:highlightedImage forState:UIControlStateHighlighted];
 [leftButton sizeToFit];

 // 按钮点击事件
 [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

 // 由于使用button会使得点击范围增大,所以将button放进view中,然后赋值
 UIView *leftView = [[UIView alloc] initWithFrame:leftButton.bounds];
 [leftView addSubview:leftButton];
 return [[UIBarButtonItem alloc] initWithCustomView:leftView];
}

+ (UIBarButtonItem *)setBackButtonItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action title:(NSString *)title
{
 // 设置返回按钮
 UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [backButton setTitle:title forState:UIControlStateNormal];
 [backButton setImage:image forState:UIControlStateNormal];
 [backButton setImage:highlightedImage forState:UIControlStateHighlighted];
 [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
 [backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
 backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
 [backButton sizeToFit];

 return [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

+ (UIBarButtonItem *)setBarButtonItemWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage target:(id)target action:(SEL)action
{
 // 1.leftBarButtonItem
 UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [leftButton setImage:image forState:UIControlStateNormal];
 [leftButton setImage:selectedImage forState:UIControlStateSelected];
 [leftButton sizeToFit];

 // 按钮点击事件
 [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

 // 由于使用button会使得点击范围增大,所以将button放进view中,然后赋值
 UIView *leftView = [[UIView alloc] initWithFrame:leftButton.bounds];
 [leftView addSubview:leftButton];
 return [[UIBarButtonItem alloc] initWithCustomView:leftView];
}

@end

上一篇:iOS多线程应用开发中使用NSOperation类的基本方法

栏    目:iOS代码

下一篇:ios下移动文件方法汇总

本文标题:iOS滑动全屏实现返回功能

本文地址:http://www.codeinn.net/misctech/116951.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有