这篇文章主要介绍了iOS如何滑动全屏实现返回功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
逊克网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联建站自2013年起到现在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如何滑动全屏实现返回功能”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
本文题目:iOS如何滑动全屏实现返回功能
文章URL:
http://chengdu.cdxwcx.cn/article/ipsogo.html