_多個ScrollView關聯(lián)

實現(xiàn)效果如下:

1、定義宏,在定義位置時候方便簡潔

// 定義整個屏幕的寬、高的宏

#define S_Width [UIScreen mainScreen].bounds.size.width

#define S_Height [UIScreen mainScreen].bounds.size.height

2、創(chuàng)建需要用的滑動條和滾動視圖,并進行互相關聯(lián)

// 定義需要用的ScrollView和滑動條

@property(nonatomic,strong)UIView *lineView;

@property(nonatomic,strong)UIScrollView *bigScrollView;

// ?創(chuàng)建按鈕

-(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{

? ? // 創(chuàng)建滾動視圖

? ? 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;

}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容