废话不多说直接上代码:
这个功能应该是挺常见的, 一个tableview到另一个tableview, 类似segment的一个东西, 我把它封装起来了:
?
|
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
//
// viewcontroller.m
//
//
// created by 高雅馨 on 16/6/3.
// copyright © 2016年 高雅馨. all rights reserved.
//
#import "dcnavtabbarcontroller.h"
#import "htmacro.h"
@interface dcnavtabbarcontroller ()<uiscrollviewdelegate>
@property (nonatomic, weak) uibutton *oldbtn;
@property(nonatomic,strong) nsarray *vcarr;
@property (nonatomic, weak) uiscrollview *contentview;
@property (nonatomic, weak) uiscrollview *topbar;
@property(nonatomic,assign) cgfloat btnw ;
@property (nonatomic, weak) uiview *slider;
@end
@implementation dcnavtabbarcontroller
-(uicolor *)slidercolor
{
if(_slidercolor == nil)
{
_slidercolor = [uicolor colorwithred:1.00 green:0.36 blue:0.25 alpha:1.00];
}
return _slidercolor;
}
-(uicolor *)btntextnomalcolor
{
if(_btntextnomalcolor == nil)
{
_btntextnomalcolor = [uicolor colorwithwhite:0.205 alpha:1.000];
}
return _btntextnomalcolor;
}
-(uicolor *)btntextseletedcolor
{
if(_btntextseletedcolor == nil)
{
_btntextseletedcolor = [uicolor colorwithred:1.00 green:0.36 blue:0.25 alpha:1.00];
}
return _btntextseletedcolor;
}
-(uicolor *)topbarcolor
{
if(_topbarcolor == nil)
{
_topbarcolor = [uicolor whitecolor];
}
return _topbarcolor;
}
-(instancetype)initwithsubviewcontrollers:(nsarray *)subviewcontrollers
{
if(self = [super init])
{
_vcarr = subviewcontrollers;
}
return self;
}
- (void)viewdidload {
[super viewdidload];
//添加上面的导航条
[self addtopbar];
//添加子控制器
[self addvcview];
//添加滑块
[self addsliderview];
}
-(void)addsliderview
{
if(self.vcarr.count == 0) return;
uiview *slider = [[uiview alloc]initwithframe:cgrectmake(25,41,self.btnw - 50, 3)];
slider.backgroundcolor = self.slidercolor;
[self.topbar addsubview:slider];
self.slider = slider;
}
-(void)addtopbar
{
if(self.vcarr.count == 0) return;
nsuinteger count = self.vcarr.count;
uiscrollview *scrollview = [[uiscrollview alloc]initwithframe:cgrectmake(0, 0, screen_width, 44)];
scrollview.backgroundcolor = self.topbarcolor;
self.topbar = scrollview;
self.topbar.bounces = no;
[self.view addsubview:self.topbar];
if(count <= 5) {
self.btnw = screen_width / count;
} else {
self.btnw = screen_width / 5.0;
}
//添加button
for (int i = 0; i<count; i++) {
uiviewcontroller *vc = self.vcarr[i];
uibutton *btn = [[uibutton alloc]initwithframe:cgrectmake(i*self.btnw, 0, self.btnw, 44)];
btn.titlelabel.font = [uifont systemfontofsize:15];
btn.titlelabel.numberoflines = 0;
btn.titlelabel.textalignment = 1;
btn.tag = 10000+i;
[btn settitlecolor:self.btntextnomalcolor forstate:uicontrolstatenormal];
[btn settitlecolor:self.btntextseletedcolor forstate:uicontrolstateselected];
[btn settitle:vc.title forstate:uicontrolstatenormal];
[btn addtarget:self action:@selector(click:) forcontrolevents:uicontroleventtouchupinside];
[self.topbar addsubview:btn];
if(i == 0)
{
btn.selected = yes;
self.oldbtn = btn;
}
}
self.topbar.contentsize = cgsizemake(self.btnw *count, -64);
}
-(void)addvcview
{
uiscrollview *contentview = [[uiscrollview alloc]initwithframe:cgrectmake(0, 0+44, screen_width, screen_height -44)];
self.contentview = contentview;
self.contentview.bounces = no;
contentview.delegate = self;
contentview.backgroundcolor = [uicolor colorwithwhite:0.859 alpha:1.000];
[self.view addsubview:contentview];
nsuinteger count = self.vcarr.count;
for (int i=0; i<count; i++) {
uiviewcontroller *vc = self.vcarr[i];
[self addchildviewcontroller:vc];
vc.view.frame = cgrectmake(i*screen_width, 0, screen_width, screen_height -44);
[contentview addsubview:vc.view];
}
contentview.contentsize = cgsizemake(count*screen_width, screen_height - 44);
contentview.pagingenabled = yes;
}
-(void)click:(uibutton *)sender
{
if(sender.selected) return;
self.oldbtn.selected = no;
sender.selected = yes;
self.contentview.contentoffset = cgpointmake((sender.tag - 10000) *screen_width, 0);
self.oldbtn.transform = cgaffinetransformidentity;
self.oldbtn = sender;
//判断导航条是否需要移动
cgfloat maxx = cgrectgetmaxx(self.slider.frame);
if(maxx >=screen_width && sender.tag != self.vcarr.count + 10000 - 1)
{
[uiview animatewithduration:0.3 animations:^{
self.topbar.contentoffset = cgpointmake(maxx - screen_width + self.btnw, -64);
}];
}else if(maxx < screen_width)
{
[uiview animatewithduration:0.3 animations:^{
self.topbar.contentoffset = cgpointmake(0, 0);
}];
}
}
-(void)scrollviewdidscroll:(uiscrollview *)scrollview
{
//滑动导航条
self.slider.frame = cgrectmake(scrollview.contentoffset.x / screen_width *self.btnw + 25 , 41, self.btnw - 50, 3);
}
//判断是否切换导航条按钮的状态
-(void)scrollviewdidenddecelerating:(uiscrollview *)scrollview
{
cgfloat offx = scrollview.contentoffset.x;
int tag = (int)(offx /screen_width + 0.5) + 10000;
uibutton *btn = [self.view viewwithtag:tag];
if(tag != self.oldbtn.tag)
{
[self click:btn];
}
}
- (void)didreceivememorywarning {
[super didreceivememorywarning];
}
@end
|
这个很容易看懂的, 是不是, 就不在这里多解释.
上面这张呢, 则是把导航栏隐藏, 自定义一个小uiview截取网络图片作为导航栏, 又自定义一个大view作为tableview头视图.并且我还运用了观察者注册消息通知, 代码有点长, 不过我写注释了哦, 可以看懂的.
?
|
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|


