实现效果如下:
1、定义宏,在定义位置时候方便简洁
// 定义整个屏幕的宽、高的宏
#define S_Width [UIScreen mainScreen].bounds.size.width
#define S_Height [UIScreen mainScreen].bounds.size.height
2、创建需要用的滑动条和滚动视图,并进行互相关联
// 定义需要用的ScrollView和滑动条
@property(nonatomic,strong)UIView *lineView;
@property(nonatomic,strong)UIScrollView *bigScrollView;
// 创建按钮
-(void)createTopButton{
NSArray *arr = @[@"第一页",@"第二页",@"第三页"];
_lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 64+50, S_Width / 3.0, 2)];
_lineView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_lineView];
for(inti =0; i < arr.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame=CGRectMake(S_Width/3.0* i ,64,S_Width/3.0,50);
[self.viewaddSubview:btn];
[btnsetTitle:arr[i] forState:UIControlStateNormal];
[btnaddTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
btn.tag=20+i;
if(i ==0) {
[btnsetTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
}
}
}
-(void)click:(UIButton*)btn{
// btn的标题:
for(inti =0; i <3; i++ ) {
UIButton*newBtn = [self.viewviewWithTag:20+i];
if(newBtn.tag== btn.tag) {
[newBtnsetTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
}else{
[newBtnsetTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}
}
// 条
_lineView.frame=CGRectMake((btn.tag-20)*S_Width/3.0,64+50,S_Width/3.0,2);
// ScrollView
[_bigScrollView setContentOffset:CGPointMake((btn.tag - 20)*S_Width, 0)];
}
-(void)createScrollView{
// 创建滚动视图
UIScrollView*bigScrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,64+52,S_Width,S_Height-64-52)];
_bigScrollView= bigScrollView;
bigScrollView.backgroundColor= [UIColorwhiteColor];
[self.viewaddSubview:bigScrollView];
bigScrollView.delegate=self;
bigScrollView.tag=10;
bigScrollView.contentSize=CGSizeMake(S_Width*3,0);
bigScrollView.pagingEnabled=YES;
}
-(void)scrollViewDidScroll:(UIScrollView*)scrollView{
CGFloatview_x =(scrollView.contentOffset.x/S_Width);
// 条:
_lineView.frame=CGRectMake(view_x*S_Width/3.0,64+50,S_Width/3.0,2);
for(inti =0; i <3; i++ ) {
UIButton*newBtn = [self.viewviewWithTag:20+i];
if(newBtn.tag -20== view_x) {
[newBtnsetTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}else{
[newBtnsetTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}
}
}
-(void)createTableView{
for(inti =0; i <3; i++) {
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(S_Width * i ,0, S_Width, _bigScrollView.bounds.size.height) style:UITableViewStylePlain];
[tableViewregisterClass:[FirstTableViewCell class] forCellReuseIdentifier:@"FirstTableViewCell"];
tableView.delegate=self;
tableView.dataSource=self;
[_bigScrollViewaddSubview:tableView];
}
}
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FirstTableViewCell"];
returncell;
}