iOSスライドフルスクリーン復帰機能


本論文の例では、iOSスライドフルスクリーンの復帰機能を実現するための具体的なコードを共有します。
システムが持っているスライドリターン機能は、スライドエッジのみで戻りますが、フルスクリーンをスライドさせてリターン機能を実現したいです。
BaseNavigationControllerを定義してUninavigationControllerを置換します。

// 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
UUBarButtonitem+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
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。