最近有個(gè)需求是關(guān)于兩個(gè)頁面切換的 剛開始想的是一個(gè)控制器控制兩個(gè)tableView然后進(jìn)行切換 但是這樣的話 一個(gè)控制器的代碼很多肋乍〗胀眩控制起來也很麻煩帆啃。然后就寫了一個(gè)小demo
如圖所示
1.創(chuàng)建 childController鱼喉。創(chuàng)建你所需要滑動跳轉(zhuǎn)的控制器
- (void)setupChildVc{
LeftTableViewController * left = [[LeftTableViewController alloc]init];
[self addChildViewController:left];
RightTableViewController * right = [[RightTableViewController alloc]init];
[self addChildViewController:right];
}
2.創(chuàng)建你的按鈕和滑動的scrollView
- (void)setupScrollView{
self.leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 30, 80, 50)];
[self.leftBtn setTitle:@"左邊左邊" forState:UIControlStateNormal];
self.leftBtn.tag = 0;
[self.leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.leftBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:self.leftBtn];
self.rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(150, 30, 80, 50)];
[self.rightBtn setTitle:@"右邊右邊" forState:UIControlStateNormal];
self.rightBtn.tag = 1;
[self.rightBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.rightBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:self.rightBtn];
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 375, 667 - 100)];
self.scrollView.pagingEnabled = YES;
self.scrollView.bounces = NO;
self.scrollView.contentSize = CGSizeMake(375 * 2, 0);
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
}
3.根據(jù)你滑動和點(diǎn)擊的按鈕去選擇顯示相應(yīng)的View
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index = scrollView.contentOffset.x / 375;
[self showVc:index];
}
- (void)showVc:(NSInteger)index {
CGFloat offsetX = index * 375;
UIViewController *vc = self.childViewControllers[index];
if (vc.isViewLoaded) {
return;
}
[self.scrollView addSubview:vc.view];
vc.view.frame = CGRectMake(offsetX, 0, 375, 567);
}
這樣就能展示出來了。代碼在下邊
鏈接:https://pan.baidu.com/s/1qZTOUwk