欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS滑动全屏实现返回功能

程序员文章站 2023-10-24 10:04:29
本文实例为大家分享了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。