-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCSementBarVC.m
142 lines (94 loc) · 3.9 KB
/
CCSementBarVC.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
//
// CCSementBarVC.m
// CCSegmentBar
//
// Created by coderchou on 2016/11/26.
// Copyright © 2016年 coderchou. All rights reserved.
//
#import "CCSementBarVC.h"
#import "UIView+CCSegmentBar.h"
@interface CCSementBarVC ()<CCSegmentBarDelegate, UIScrollViewDelegate>
@property (nonatomic, weak) UIScrollView *contentView;
@end
@implementation CCSementBarVC
- (CCSegmentBar *)segmentBar {
if (!_segmentBar) {
CCSegmentBar *segmentBar = [CCSegmentBar segmentBarWithFrame:CGRectZero];
segmentBar.delegate = self;
segmentBar.backgroundColor = [UIColor brownColor];
[self.view addSubview:segmentBar];
_segmentBar = segmentBar;
}
return _segmentBar;
}
- (UIScrollView *)contentView {
if (!_contentView) {
UIScrollView *contentView = [[UIScrollView alloc] init];
contentView.delegate = self;
contentView.pagingEnabled = YES;
[self.view addSubview:contentView];
_contentView = contentView;
}
return _contentView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
}
- (void)setUpWithItems: (NSArray <NSString *>*)items childVCs: (NSArray <UIViewController *>*)childVCs {
NSAssert(items.count != 0 || items.count == childVCs.count, @"个数不一致, 请自己检查");
self.segmentBar.items = items;
[self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
// 添加几个自控制器
// 在contentView, 展示子控制器的视图内容
for (UIViewController *vc in childVCs) {
[self addChildViewController:vc];
}
//
self.contentView.contentSize = CGSizeMake(items.count * self.view.width, 0);
self.segmentBar.selectIndex = 0;
}
- (void)showChildVCViewsAtIndex: (NSInteger)index {
if (self.childViewControllers.count == 0 || index < 0 || index > self.childViewControllers.count - 1) {
return;
}
UIViewController *vc = self.childViewControllers[index];
vc.view.frame = CGRectMake(index * self.contentView.width, 0, self.contentView.width, self.contentView.height);
[self.contentView addSubview:vc.view];
// 滚动到对应的位置
[self.contentView setContentOffset:CGPointMake(index * self.contentView.width, 0) animated:YES];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
if (self.segmentBar.superview == self.view) {
self.segmentBar.frame = CGRectMake(0, 60, self.view.width, 35);
CGFloat contentViewY = self.segmentBar.y + self.segmentBar.height;
CGRect contentFrame = CGRectMake(0, contentViewY, self.view.width, self.view.height - contentViewY);
self.contentView.frame = contentFrame;
self.contentView.contentSize = CGSizeMake(self.childViewControllers.count * self.view.width, 0);
return;
}
CGRect contentFrame = CGRectMake(0, 0,self.view.width,self.view.height);
self.contentView.frame = contentFrame;
self.contentView.contentSize = CGSizeMake(self.childViewControllers.count * self.view.width, 0);
// 其他的控制器视图, 大小
// 遍历所有的视图, 重新添加, 重新进行布局
// 注意: 1个视图
//
self.segmentBar.selectIndex = self.segmentBar.selectIndex;
}
#pragma mark - 选项卡代理方法
- (void)segmentBar:(CCSegmentBar *)segmentBar didSelectIndex:(NSInteger)toIndex fromIndex:(NSInteger)fromIndex
{
NSLog(@"%zd----%zd", fromIndex, toIndex);
[self showChildVCViewsAtIndex:toIndex];
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// 计算最后的索引
NSInteger index = self.contentView.contentOffset.x / self.contentView.width;
// [self showChildVCViewsAtIndex:index];
self.segmentBar.selectIndex = index;
}
@end