设为首页 加入收藏

TOP

内外分离接口依赖及UIScrollView知识点(三)
2017-10-13 10:33:43 】 浏览:9000
Tags:内外 分离 接口 依赖 UIScrollView 知识点
roller alloc] init]; readerVc.title
= @"订阅"; [self addChildViewController:readerVc]; ScienceViewController *scienceVc = [[ScienceViewController alloc] init]; scienceVc.title = @"科技"; [self addChildViewController:scienceVc]; } // 设置标题 - (void)setUpTitle { NSUInteger count = self.childViewControllers.count; CGFloat btnX = 0; CGFloat btnY = 0; CGFloat btnW = 100; CGFloat btnH = 44; for (NSUInteger i = 0; i < count; i++) { btnX = i * btnW; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.tag = i; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; UIViewController *vc = self.childViewControllers[i]; [btn setTitle:vc.title forState:UIControlStateNormal]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; btn.frame = CGRectMake(btnX, btnY, btnW, btnH); [btn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside]; [self.titileScrollView addSubview:btn]; [self.titleBtns addObject:btn]; // 默认选中第一个 if (i == 0) { [self titleClick:btn]; } } self.titileScrollView.contentSize = CGSizeMake(btnW * count, 0); } // 点击标题按钮 - (void)titleClick:(UIButton *)btn { // 0.设置标题按钮居中 [self setUpTitleBtnMiddle:btn]; // 1.滚动到对应的界面 CGFloat offsetX = btn.tag * ScreenW; self.contentView.contentOffset = CGPointMake(offsetX, 0); // 2.添加对应子控制器view到对应的位置 UIViewController *vc = self.childViewControllers[btn.tag]; vc.view.frame = CGRectMake(offsetX, 0, ScreenW, self.contentView.bounds.size.height); // NSLog(@"%@",NSStringFromCGRect(self.contentView.bounds)); [self.contentView addSubview:vc.view]; // 还原之前标题的形变 [self setSelectBtn:btn]; } - (void)setSelectBtn:(UIButton *)btn { _selButton.transform = CGAffineTransformIdentity; [_selButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _selButton.selected = NO; btn.transform = CGAffineTransformMakeScale(1.3, 1.3); btn.selected = YES; _selButton = btn; } #pragma mark - UIScrollViewDelegate // 减速完成的时候 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { // 获取当前的偏移量 CGFloat offsetX = scrollView.contentOffset.x; // 获取页码 int page = offsetX / scrollView.bounds.size.width; UIButton *btn = self.titileScrollView.subviews[page]; // 选中按钮 [self setSelectBtn:btn]; // 设置按钮居中 [self setUpTitleBtnMiddle:btn]; // 添加对应子控制器到界面上 UIViewController *vc = self.childViewControllers[page]; // 已经加载的view就不需要添加了 if (vc.isViewLoaded) return; vc.view.frame = CGRectMake(offsetX , 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height); [self.contentView addSubview:vc.view]; } // 设置标题按钮居中 - (void)setUpTitleBtnMiddle:(UIButton *)btn { // 计算偏移量 CGFloat offsetX = btn.center.x - ScreenW * 0.5; // 左边偏移多了,表示需要往左边看,可视范围往左边,偏移量就减少,最少应该是0 if (offsetX < 0) offsetX = 0; CGFloat maxOffsetX = self.titileScrollView.contentSize.width - ScreenW; // 右边偏移多了,表示需要往右边看,可视范围往又边,偏移量就增加,最大不超过内容范围 - 屏幕宽度 if (offsetX > maxOffsetX) offsetX = maxOffsetX; [self.titileScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES]; } // 监听内容v
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇仿UC浏览器图片加载进度条 下一篇整体开源源代码记录(滚动图片、滚..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目