-
Notifications
You must be signed in to change notification settings - Fork 1
/
MRJ_TabBarController.m
501 lines (395 loc) · 16.7 KB
/
MRJ_TabBarController.m
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//
// MRJ_TabBarController.m
// MRJ_TabBarController
//
// Created by 余洪江 on 17/8/11.
// Copyright (c) 2017年 MRJ_TabBarController. All rights reserved.
//
#import "MRJ_TabBarController.h"
#import <objc/runtime.h>
#define TAB_BAR_HEIGHT 50
#pragma mark - MRJ_TabContentScrollView
/**
* 自定义UIScrollView,在需要时可以拦截其滑动手势
*/
@class MRJ_TabContentScrollView;
@protocol MRJ_TabContentScrollViewDelegate <NSObject>
@optional
- (BOOL)scrollView:(MRJ_TabContentScrollView *)scrollView shouldScrollToPageIndex:(NSUInteger)index;
@end
@interface MRJ_TabContentScrollView : UIScrollView
@property (nonatomic, weak) id<MRJ_TabContentScrollViewDelegate> MRJ__delegate;
@property (nonatomic, assign) BOOL interceptLeftSlideGuetureInLastPage;
@property (nonatomic, assign) BOOL interceptRightSlideGuetureInFirstPage;
@end
#pragma mark - UIViewController (MRJ_TabBarController)
@implementation UIViewController (MRJ_TabBarController)
- (NSString *)mrj_tabItemTitle {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setMrj_tabItemTitle:(NSString *)mrj_tabItemTitle {
self.mrj_tabItem.title = mrj_tabItemTitle;
objc_setAssociatedObject(self, @selector(mrj_tabItemTitle), mrj_tabItemTitle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (UIImage *)mrj_tabItemImage {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setMrj_tabItemImage:(UIImage *)mrj_tabItemImage {
self.mrj_tabItem.image = mrj_tabItemImage;
objc_setAssociatedObject(self, @selector(mrj_tabItemImage), mrj_tabItemImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImage *)mrj_tabItemSelectedImage {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setMrj_tabItemSelectedImage:(UIImage *)mrj_tabItemSelectedImage {
self.mrj_tabItem.selectedImage = mrj_tabItemSelectedImage;
objc_setAssociatedObject(self, @selector(mrj_tabItemSelectedImage), mrj_tabItemSelectedImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (MRJ_TabItem *)mrj_tabItem {
MRJ_TabBar *tabBar = self.mrj_tabBarController.tabBar;
if (!tabBar) {
return nil;
}
if (![self.mrj_tabBarController.viewControllers containsObject:self]) {
return nil;
}
NSUInteger index = [self.mrj_tabBarController.viewControllers indexOfObject:self];
return tabBar.items[index];
}
- (MRJ_TabBarController *)mrj_tabBarController {
return (MRJ_TabBarController *)self.parentViewController;
}
- (void)mrj_tabItemDidSelected:(BOOL)isFirstTime {}
- (void)tabItemDidSelected {}
- (void)mrj_tabItemDidDeselected {}
- (void)tabItemDidDeselected {}
- (BOOL)mrj_isTabItemSelectedFirstTime {
id selected = objc_getAssociatedObject(self, _cmd);
if (!selected) {
return YES;
}
return [selected boolValue];
}
@end
#pragma mark - MRJ_TabBarController
@interface MRJ_TabBarController () <UIScrollViewDelegate, MRJ_TabContentScrollViewDelegate> {
BOOL _didViewAppeared;
CGFloat _lastContentScrollViewOffsetX;
}
@property (nonatomic, strong) MRJ_TabContentScrollView *contentScrollView;
@property (nonatomic, assign) BOOL contentScrollEnabled;
@property (nonatomic, assign) BOOL contentSwitchAnimated;
@end
@implementation MRJ_TabBarController
- (instancetype)init {
self = [super init];
if (self) {
[self _setup];
}
return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self _setup];
}
return self;
}
- (void)_setup {
_selectedControllerIndex = NSNotFound;
_tabBar = [[MRJ_TabBar alloc] init];
_tabBar.delegate = self;
_loadViewOfChildContollerWhileAppear = NO;
_defaultSelectedControllerIndex = 0;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
self.view.clipsToBounds = YES;
self.view.backgroundColor = [UIColor whiteColor];
[self setupFrameOfTabBarAndContentView];
[self.view addSubview:self.tabBar];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 在第一次调用viewWillAppear方法时,初始化选中的item
if (!_didViewAppeared) {
self.tabBar.selectedItemIndex = self.defaultSelectedControllerIndex;
_didViewAppeared = YES;
}
}
- (void)setupFrameOfTabBarAndContentView {
// 设置默认的tabBar的frame和contentViewFrame
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat contentViewY = 0;
CGFloat tabBarY = screenSize.height - TAB_BAR_HEIGHT;
CGFloat contentViewHeight = tabBarY;
// 如果parentViewController为UINavigationController及其子类
if ([self.parentViewController isKindOfClass:[UINavigationController class]] &&
!self.navigationController.navigationBarHidden &&
!self.navigationController.navigationBar.hidden) {
CGFloat navMaxY = CGRectGetMaxY(self.navigationController.navigationBar.frame);
if (!self.navigationController.navigationBar.translucent ||
self.edgesForExtendedLayout == UIRectEdgeNone ||
self.edgesForExtendedLayout == UIRectEdgeTop) {
tabBarY = screenSize.height - TAB_BAR_HEIGHT - navMaxY;
contentViewHeight = tabBarY;
} else {
contentViewY = navMaxY;
contentViewHeight = screenSize.height - TAB_BAR_HEIGHT - contentViewY;
}
}
[self setTabBarFrame:CGRectMake(0, tabBarY, screenSize.width, TAB_BAR_HEIGHT)
contentViewFrame:CGRectMake(0, contentViewY, screenSize.width, contentViewHeight)];
}
- (void)setContentViewFrame:(CGRect)contentViewFrame {
_contentViewFrame = contentViewFrame;
[self updateContentViewsFrame];
}
- (void)setTabBarFrame:(CGRect)tabBarFrame contentViewFrame:(CGRect)contentViewFrame {
self.tabBar.frame = tabBarFrame;
self.contentViewFrame = contentViewFrame;
}
- (void)setViewControllers:(NSArray *)viewControllers {
for (UIViewController *controller in _viewControllers) {
[controller removeFromParentViewController];
if (controller.isViewLoaded) {
[controller.view removeFromSuperview];
}
}
_viewControllers = [viewControllers copy];
NSMutableArray *items = [NSMutableArray array];
for (UIViewController *controller in _viewControllers) {
[self addChildViewController:controller];
MRJ_TabItem *item = [MRJ_TabItem buttonWithType:UIButtonTypeCustom];
item.image = controller.mrj_tabItemImage;
item.selectedImage = controller.mrj_tabItemSelectedImage;
item.title = controller.mrj_tabItemTitle;
[items addObject:item];
}
self.tabBar.items = items;
if (_didViewAppeared) {
_selectedControllerIndex = NSNotFound;
self.tabBar.selectedItemIndex = 0;
}
// 更新scrollView的content size
if (self.contentScrollView) {
self.contentScrollView.contentSize = CGSizeMake(self.contentViewFrame.size.width * _viewControllers.count,
self.contentViewFrame.size.height);
}
}
- (void)setContentScrollEnabledAndTapSwitchAnimated:(BOOL)switchAnimated {
if (!self.contentScrollView) {
self.contentScrollView = [[MRJ_TabContentScrollView alloc] initWithFrame:self.contentViewFrame];
#if TARGET_OS_IOS
self.contentScrollView.pagingEnabled = YES;
self.contentScrollView.scrollsToTop = NO;
#elif TARGET_OS_TV
#endif
self.contentScrollView.showsHorizontalScrollIndicator = NO;
self.contentScrollView.showsVerticalScrollIndicator = NO;
self.contentScrollView.delegate = self;
self.contentScrollView.MRJ__delegate = self;
[self.view insertSubview:self.contentScrollView belowSubview:self.tabBar];
self.contentScrollView.contentSize = CGSizeMake(self.contentViewFrame.size.width * self.viewControllers.count, self.contentViewFrame.size.height);
}
[self updateContentViewsFrame];
self.contentSwitchAnimated = switchAnimated;
}
- (void)updateContentViewsFrame {
if (self.contentScrollView) {
self.contentScrollView.frame = self.contentViewFrame;
self.contentScrollView.contentSize = CGSizeMake(self.contentViewFrame.size.width * self.viewControllers.count,
self.contentViewFrame.size.height);
[self.viewControllers enumerateObjectsUsingBlock:^(UIViewController * _Nonnull controller,
NSUInteger idx, BOOL * _Nonnull stop) {
if (controller.isViewLoaded) {
controller.view.frame = [self frameForControllerAtIndex:idx];
}
}];
[self.contentScrollView scrollRectToVisible:self.selectedController.view.frame animated:NO];
} else {
self.selectedController.view.frame = self.contentViewFrame;
}
}
- (CGRect)frameForControllerAtIndex:(NSUInteger)index {
return CGRectMake(index * self.contentViewFrame.size.width,
0,
self.contentViewFrame.size.width,
self.contentViewFrame.size.height);
}
- (void)setInterceptRightSlideGuetureInFirstPage:(BOOL)interceptRightSlideGuetureInFirstPage {
_interceptRightSlideGuetureInFirstPage = interceptRightSlideGuetureInFirstPage;
self.contentScrollView.interceptRightSlideGuetureInFirstPage = interceptRightSlideGuetureInFirstPage;
}
- (void)setInterceptLeftSlideGuetureInLastPage:(BOOL)interceptLeftSlideGuetureInLastPage {
_interceptLeftSlideGuetureInLastPage = interceptLeftSlideGuetureInLastPage;
self.contentScrollView.interceptLeftSlideGuetureInLastPage = interceptLeftSlideGuetureInLastPage;
}
- (void)setSelectedControllerIndex:(NSUInteger)selectedControllerIndex {
self.tabBar.selectedItemIndex = selectedControllerIndex;
}
- (UIViewController *)selectedController {
if (self.selectedControllerIndex != NSNotFound) {
return self.viewControllers[self.selectedControllerIndex];
}
return nil;
}
- (void)didSelectViewControllerAtIndex:(NSUInteger)index {}
#pragma mark - MRJ_TabBarDelegate
- (void)MRJ__tabBar:(MRJ_TabBar *)tabBar didSelectedItemAtIndex:(NSUInteger)index {
if (index == self.selectedControllerIndex) {
return;
}
UIViewController *oldController = nil;
if (self.selectedControllerIndex != NSNotFound) {
oldController = self.viewControllers[self.selectedControllerIndex];
[oldController mrj_tabItemDidDeselected];
if ([oldController respondsToSelector:@selector(tabItemDidDeselected)]) {
[oldController performSelector:@selector(tabItemDidDeselected)];
}
[self.viewControllers enumerateObjectsUsingBlock:^(UIViewController * _Nonnull controller, NSUInteger idx, BOOL * _Nonnull stop) {
if (idx != index && controller.isViewLoaded && controller.view.superview) {
[controller.view removeFromSuperview];
}
}];
}
UIViewController *curController = self.viewControllers[index];
if (self.contentScrollView) {
// contentView支持滚动
if (!curController.isViewLoaded) {
curController.view.frame = [self frameForControllerAtIndex:index];
}
[self.contentScrollView addSubview:curController.view];
// 切换到curController
[self.contentScrollView scrollRectToVisible:curController.view.frame animated:self.contentSwitchAnimated];
} else {
// contentView不支持滚动
[self.view insertSubview:curController.view belowSubview:self.tabBar];
// 设置curController.view的frame
if (!CGRectEqualToRect(curController.view.frame, self.contentViewFrame)) {
curController.view.frame = self.contentViewFrame;
}
}
BOOL isSelectedFirstTime = [curController mrj_isTabItemSelectedFirstTime];
if (isSelectedFirstTime) {
objc_setAssociatedObject(curController, @selector(mrj_isTabItemSelectedFirstTime), @(NO), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
[curController mrj_tabItemDidSelected:isSelectedFirstTime];
if ([curController respondsToSelector:@selector(tabItemDidSelected)]) {
[curController performSelector:@selector(tabItemDidSelected)];
}
// 当contentView为scrollView及其子类时,设置它支持点击状态栏回到顶部
if (oldController && [oldController.view isKindOfClass:[UIScrollView class]]) {
#if TARGET_OS_IOS
[(UIScrollView *)oldController.view setScrollsToTop:NO];
#elif TARGET_OS_TV
#endif
}
if ([curController.view isKindOfClass:[UIScrollView class]]) {
#if TARGET_OS_IOS
[(UIScrollView *)curController.view setScrollsToTop:YES];
#elif TARGET_OS_TV
#endif
}
_selectedControllerIndex = index;
[self didSelectViewControllerAtIndex:_selectedControllerIndex];
}
#pragma mark - MRJ_TabContentScrollViewDelegate
- (BOOL)scrollView:(MRJ_TabContentScrollView *)scrollView shouldScrollToPageIndex:(NSUInteger)index {
if ([self respondsToSelector:@selector(tabBar:shouldSelectItemAtIndex:)]) {
return [self tabBar:self.tabBar shouldSelectItemAtIndex:index];
}
return YES;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSUInteger page = scrollView.contentOffset.x / scrollView.frame.size.width;
self.tabBar.selectedItemIndex = page;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 如果不是手势拖动导致的此方法被调用,不处理
if (!(scrollView.isDragging || scrollView.isDecelerating)) {
return;
}
// 滑动越界不处理
CGFloat offsetX = scrollView.contentOffset.x;
CGFloat scrollViewWidth = scrollView.frame.size.width;
if (offsetX < 0) {
return;
}
if (offsetX > scrollView.contentSize.width - scrollViewWidth) {
return;
}
NSUInteger leftIndex = offsetX / scrollViewWidth;
NSUInteger rightIndex = leftIndex + 1;
// 这里处理shouldSelectItemAtIndex方法
if ([self respondsToSelector:@selector(tabBar:shouldSelectItemAtIndex:)] && !scrollView.isDecelerating) {
NSUInteger targetIndex;
if (_lastContentScrollViewOffsetX < (CGFloat)offsetX) {
// 向左
targetIndex = rightIndex;
} else {
// 向右
targetIndex = leftIndex;
}
if (targetIndex != self.selectedControllerIndex) {
if (![self tabBar:self.tabBar shouldSelectItemAtIndex:targetIndex]) {
[scrollView setContentOffset:CGPointMake(self.selectedControllerIndex * scrollViewWidth, 0) animated:NO];
}
}
}
_lastContentScrollViewOffsetX = offsetX;
// 刚好处于能完整显示一个child view的位置
if (leftIndex == offsetX / scrollViewWidth) {
rightIndex = leftIndex;
}
// 将需要显示的child view放到scrollView上
for (NSUInteger index = leftIndex; index <= rightIndex; index++) {
UIViewController *controller = self.viewControllers[index];
if (!controller.isViewLoaded && self.loadViewOfChildContollerWhileAppear) {
controller.view.frame = [self frameForControllerAtIndex:index];
}
if (controller.isViewLoaded && !controller.view.superview) {
[self.contentScrollView addSubview:controller.view];
}
}
// 同步修改tarBar的子视图状态
[self.tabBar updateSubViewsWhenParentScrollViewScroll:self.contentScrollView];
}
@end
@implementation MRJ_TabContentScrollView
/**
* 重写此方法,在需要的时候,拦截UIPanGestureRecognizer
*/
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
if (![gestureRecognizer respondsToSelector:@selector(translationInView:)]) {
return YES;
}
// 计算可能切换到的index
NSInteger currentIndex = self.contentOffset.x / self.frame.size.width;
NSInteger targetIndex = currentIndex;
CGPoint translation = [gestureRecognizer translationInView:self];
if (translation.x > 0) {
targetIndex = currentIndex - 1;
} else {
targetIndex = currentIndex + 1;
}
// 第一页往右滑动
if (self.interceptRightSlideGuetureInFirstPage && targetIndex < 0) {
return NO;
}
// 最后一页往左滑动
if (self.interceptLeftSlideGuetureInLastPage) {
NSUInteger numberOfPage = self.contentSize.width / self.frame.size.width;
if (targetIndex >= numberOfPage) {
return NO;
}
}
// 其他情况
if (self.MRJ__delegate && [self.MRJ__delegate respondsToSelector:@selector(scrollView:shouldScrollToPageIndex:)]) {
return [self.MRJ__delegate scrollView:self shouldScrollToPageIndex:targetIndex];
}
return YES;
}
@end