您好,登錄后才能下訂單哦!
本文實例為大家分享了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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。