网站建设资讯

NEWS

网站建设资讯

iOS滑动全屏实现返回功能

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

10年积累的网站制作、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有颍上免费网站建设让你可以放心的选择与我们合作。

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

定义BaseNavigationController来取代UINavigationController

// BaseNavigationController.h
#import 

@interface BaseNavigationController : UINavigationController

@end

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

@interface BaseNavigationController () 

@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 : ; 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 

@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滑动全屏实现返回功能
URL链接:http://cdweb.net/article/ppjiho.html