ScrollView分栏视图分析
代码精华部分如下:
//1.添加scrollView /* 1). 添加一个scrollView; 2).创建关注、热门、附近三个控制器。 3). 存储它们的名字到数组中,遍历并放到本控制器里。 4).设置scrollView的contentSize. 5).设置scrollview的偏移量。 6).调用当停止动画的方法设置为当前的scrollView.(应该是停止拖拽的方法)*/ NSArray *controls = @[@"STRFollowViewController",@"STRHotViewController",@"STRNearViewController"]; for (NSInteger i = 0; i < controls.count; i++) { UIViewController *vc = [[NSClassFromString(controls[i]) alloc] init]; vc.title = self.titleNames[i]; [self addChildViewController:vc]; } self.MainScrollView.contentSize = CGSizeMake(self.titleNames.count *[UIScreen mainScreen].bounds.size.width , 0); self.MainScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0); [self scrollViewDidEndDecelerating:self.MainScrollView]; } //2.设置代理 #pragma mark scrollViewDelegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ [self scrollViewDidEndScrollingAnimation:scrollView]; } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ /** 1. 获取宽度和高度,还要打开分页功能 2. 获取索引值。 3. 设置偏移量 4. 设置viewframe,如没有加载过再加载,否则不返回 5. 添加子控制器view到scrollview上*/ NSInteger width = SCREEN_WIDTH; NSInteger height = SCREEN_HEIGHT; CGFloat offsetX = scrollView.contentOffset.x; NSInteger index = offsetX/width; [self.topView topViewWithIndex:index]; UIViewController *vc = self.childViewControllers[index]; if ([vc isViewLoaded]) return; vc.view.frame = CGRectMake(offsetX, 0, width, height); [self.MainScrollView addSubview:vc.view]; } //3.添加topView - (STRTopView *)topView{ if (!_topView) { _topView = [[STRTopView alloc] initWithFrame:CGRectMake(0, 0, 200, 44) titleNames:self.titleNames tagBlock:^(NSInteger index) { [self.MainScrollView setContentOffset:CGPointMake(index *SCREEN_WIDTH, 0) animated:YES]; }]; } return _topView; }
将来的自己,会感谢现在不放弃的自己!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现