最近有个需求是关于两个页面切换的 刚开始想的是一个控制器控制两个tableView然后进行切换 但是这样的话 一个控制器的代码很多。控制起来也很麻烦。然后就写了一个小demo
如图所示
1.创建 childController。创建你所需要滑动跳转的控制器
- (void)setupChildVc{
LeftTableViewController * left = [[LeftTableViewController alloc]init];
[self addChildViewController:left];
RightTableViewController * right = [[RightTableViewController alloc]init];
[self addChildViewController:right];
}
2.创建你的按钮和滑动的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.根据你滑动和点击的按钮去选择显示相应的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