-
Notifications
You must be signed in to change notification settings - Fork 0
/
EXUScrollTabController.m
188 lines (150 loc) · 6.55 KB
/
EXUScrollTabController.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
//
// EXUScrollTabController.m
// chacha
//
// Created by 王广威 on 2017/2/28.
// Copyright © 2017年 EXUTECH. All rights reserved.
//
#import "EXUScrollTabController.h"
#import <objc/runtime.h>
static NSString * const ID = @"CONTENTCELL";
#define EXUScreenW ([[UIScreen mainScreen] bounds].size.width)
#define EXUScreenH ([[UIScreen mainScreen] bounds].size.height)
@interface EXUScrollTabController ()<UICollectionViewDataSource, UICollectionViewDelegate, UINavigationControllerDelegate>
/** 内容滚动视图 */
@property (nonatomic, weak) UICollectionView *contentScrollView;
/** 当前选中的控制器 */
@property (nonatomic, weak) UIViewController *currentController;
/** 当前过渡的控制器 */
@property (nonatomic, weak) UIViewController *tempController;
@end
@implementation EXUScrollTabController
#pragma mark - 初始化
- (instancetype)initWithViewControllers:(NSArray<UIViewController *> *)viewControllers {
if (self = [super init]) {
[viewControllers enumerateObjectsUsingBlock:^(UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self addChildViewController:obj];
if ([obj isKindOfClass:[UINavigationController class]]) {
UINavigationController *naviObj = (UINavigationController *)obj;
if (!naviObj.delegate) {
naviObj.delegate = self;
}
}
}];
}
return self;
}
#pragma mark - 控制器view生命周期方法
- (void)loadView {
self.view = self.contentScrollView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
self.contentScrollView.frame = CGRectMake(0, 0, EXUScreenW, EXUScreenH);
self.contentScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.contentScrollView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
self.currentController = self.childViewControllers[self.currentIndex];
}
#pragma mark - 懒加载
// 懒加载内容滚动视图
- (UIScrollView *)contentScrollView {
if (_contentScrollView == nil) {
// 创建布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
layout.itemSize = CGSizeMake(EXUScreenW, EXUScreenH);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
UICollectionView *contentScrollView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
contentScrollView.backgroundColor = [UIColor whiteColor];
// 设置内容滚动视图
contentScrollView.pagingEnabled = YES;
contentScrollView.showsHorizontalScrollIndicator = NO;
contentScrollView.showsVerticalScrollIndicator = NO;
contentScrollView.delegate = self;
contentScrollView.dataSource = self;
// 注册cell
[contentScrollView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
_contentScrollView = contentScrollView;
}
return _contentScrollView;
}
#pragma mark - 设置当前选中的控制器
- (void)setCurrentIndex:(NSInteger)currentIndex {
if (_currentIndex != currentIndex) {
_currentIndex = currentIndex;
if (self.viewLoaded) {
// 内容滚动视图滚动到对应位置
[self scrollToIndex:currentIndex animated:YES];
}
}
}
- (void)setScrollEnabled:(BOOL)scrollEnabled {
self.contentScrollView.scrollEnabled = scrollEnabled;
}
- (void)scrollToIndex:(NSInteger)targetIndex animated:(BOOL)animated {
[self.contentScrollView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:animated];
}
#pragma mark - 刷新界面方法
// 更新界面
- (void)refreshDisplay {
[self.contentScrollView reloadData];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.childViewControllers.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *vc = self.childViewControllers[indexPath.item];
[vc beginAppearanceTransition:YES animated:NO];
[cell.contentView addSubview:vc.view];
vc.view.frame = cell.contentView.bounds;
vc.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tempController = vc;
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *vc = self.childViewControllers[indexPath.item];
[vc.view removeFromSuperview];
[vc endAppearanceTransition];
self.tempController = nil;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[_currentController beginAppearanceTransition:NO animated:NO];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
_currentIndex = scrollView.contentOffset.x / EXUScreenW;
_currentController = self.childViewControllers[self.currentIndex];
[_currentController endAppearanceTransition];
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
CGPoint targetOffset = *targetContentOffset;
NSUInteger targetIndex = targetOffset.x / EXUScreenW;
UIViewController *targetVC = self.childViewControllers[targetIndex];
if (targetVC == self.currentController) {
[_currentController endAppearanceTransition];
[_currentController beginAppearanceTransition:YES animated:NO];
[_tempController endAppearanceTransition];
[_tempController beginAppearanceTransition:NO animated:NO];
}
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
self.scrollEnabled = [navigationController.viewControllers firstObject] == viewController;
}
@end
@implementation UIViewController (EXUScrollTabController)
- (EXUScrollTabController *)scrollTabController {
UIResponder *scrollTabController = self.nextResponder;
while (scrollTabController) {
if ([scrollTabController isKindOfClass:[EXUScrollTabController class]]) {
return (EXUScrollTabController *)scrollTabController;
}
scrollTabController = scrollTabController.nextResponder;
}
return nil;
}
@end