服务器之家

服务器之家 > 正文

iOS滑动全屏实现返回功能

时间:2021-05-25 15:43     来源/作者:抬头看见柠檬树

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

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

定义basenavigationcontroller来取代uinavigationcontroller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 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

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

原文链接:https://blog.csdn.net/MyKingSaber/article/details/72841339

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部