本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。