转:addChildViewController实现网易新闻首页切换
本来只是打算介绍一下addChildViewController这个方法的,正好今天朋友去换工作面试问到网易新闻标签栏效果的实现,就结合它,用个小Demo实例介绍一下:(具体解释都写在了Demo里面的注释)
- //
- // HMTMainViewController.m
- // UIScrollView
- //
- // Created by HMT on 14-6-25.
- // Copyright (c) 2014年 humingtao. All rights reserved.
- //
- #import "HMTMainViewController.h"
- #import "HMTFirstViewController.h"
- #import "HMTSecondViewController.h"
- #import "HMTThirdViewController.h"
- @interface HMTMainViewController () <UIScrollViewDelegate>
- @property (nonatomic ,strong) HMTThirdViewController *thirdVC;
- @property (nonatomic ,strong) HMTFirstViewController *firstVC;
- @property (nonatomic ,strong) HMTSecondViewController *secondVC;
- @property (nonatomic ,strong) UIViewController *currentVC;
- @property (nonatomic ,strong) UIScrollView *headScrollView; // 顶部滚动视图
- @property (nonatomic ,strong) NSArray *headArray;
- @end
- @implementation HMTMainViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.navigationItem.title = @"网易新闻Demo";
- self.headArray = @[@"头条",@"娱乐",@"体育",@"财经",@"科技",@"NBA",@"手机"];
- /**
- * automaticallyAdjustsScrollViewInsets 又被这个属性坑了
- * 我"UI高级"里面一篇文章着重讲了它,大家可以去看看
- */
- self.automaticallyAdjustsScrollViewInsets = NO;
- self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, 320, 40)];
- self.headScrollView.backgroundColor = [UIColor purpleColor];
- self.headScrollView.contentSize = CGSizeMake(560, 0);
- self.headScrollView.bounces = NO;
- self.headScrollView.pagingEnabled = YES;
- [self.view addSubview:self.headScrollView];
- for (int i = 0; i < [self.headArray count]; i++) {
- UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
- button.frame = CGRectMake(0 + i*80, 0, 80, 40);
- [button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];
- button.tag = i + 100;
- [button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- [self.headScrollView addSubview:button];
- }
- /*
- 苹果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用[self addChildViewController:child]方法将sub view对应的viewController也加到当前ViewController的管理中。
- 对于那些当前暂时不需要显示的subview,只通过addChildViewController把subViewController加进去;需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。
- 这样做的好处:
- 1.无疑,对页面中的逻辑更加分明了。相应的View对应相应的ViewController。
- 2.当某个子View没有显示时,将不会被Load,减少了内存的使用。
- 3.当内存紧张时,没有Load的View将被首先释放,优化了程序的内存释放机制。
- */
- /**
- * 在iOS5中,ViewController中新添加了下面几个方法:
- * addChildViewController:
- * removeFromParentViewController
- * transitionFromViewController:toViewController:duration:options:animations:completion:
- * willMoveToParentViewController:
- * didMoveToParentViewController:
- */
- self.firstVC = [[HMTFirstViewController alloc] init];
- [self.firstVC.view setFrame:CGRectMake(0, 104, 320, 464)];
- [self addChildViewController:_firstVC];
- self.secondVC = [[HMTSecondViewController alloc] init];
- [self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)];
- self.thirdVC = [[HMTThirdViewController alloc] init];
- [self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)];
- // 默认,第一个视图(你会发现,全程就这一个用了addSubview)
- [self.view addSubview:self.firstVC.view];
- self.currentVC = self.firstVC;
- }
- - (void)didClickHeadButtonAction:(UIButton *)button
- {
- // 点击处于当前页面的按钮,直接跳出
- if ((self.currentVC == self.firstVC && button.tag == 100)||(self.currentVC == self.secondVC && button.tag == 101.)) {
- return;
- }else{
- // 展示2个,其余一样,自行补全噢
- switch (button.tag) {
- case 100:
- [self replaceController:self.currentVC newController:self.firstVC];
- break;
- case 101:
- [self replaceController:self.currentVC newController:self.secondVC];
- break;
- case 102:
- //.......
- break;
- case 103:
- //.......
- break;
- case 104:
- //.......
- break;
- case 105:
- //.......
- break;
- case 106:
- //.......
- break;
- //.......
- default:
- break;
- }
- }
- }
- // 切换各个标签内容
- - (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
- {
- /**
- * 着重介绍一下它
- * transitionFromViewController:toViewController:duration:options:animations:completion:
- * fromViewController 当前显示在父视图控制器中的子视图控制器
- * toViewController 将要显示的姿势图控制器
- * duration 动画时间(这个属性,old friend 了 O(∩_∩)O)
- * options 动画效果(渐变,从下往上等等,具体查看API)
- * animations 转换过程中得动画
- * completion 转换完成
- */
- [self addChildViewController:newController];
- [self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
- if (finished) {
- [newController didMoveToParentViewController:self];
- [oldController willMoveToParentViewController:nil];
- [oldController removeFromParentViewController];
- self.currentVC = newController;
- }else{
- self.currentVC = oldController;
- }
- }];
- }
转:addChildViewController实现网易新闻首页切换的更多相关文章
- 网易新闻首页iOS
// // ViewController.m // wyy // // Copyright © 2016年 zm. All rights reserved. // #import "V ...
- IOS 类似于网易新闻首页新闻轮播的组件
一.需求分析 1.可横向循环滚动新闻图片 2.滚动到对应图片时显示新闻标题 3.每张新闻图片可点击 4.有pageControl提示 5.具有控件的扩展能力 二.设计实现 1.显示图片使用SDWebI ...
- android 仿网易新闻首页框架
实现思路很简单左侧栏目是一个一个的 Fragment 的,点击时动态替换各个 Fragment 到当前 Activity 中. 关键代码: public void loadFragment(Ma ...
- 【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...
- iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
本文转载至 http://www.tuicool.com/articles/3ymMzub CSDN博客原文 http://blog.csdn.net/hmt20130412/article/det ...
- ActionBar+DrawerLayout实现网易新闻客户端首页
一.概述 随着android版本的不断的更新,google推出了越来越多的高级组件,采用这些官方组件我们可以方便的实现一些以前需要通过复杂编码或者使用第三方组件才能实现的效果,比如slidingmen ...
- 基于VUE,VUX组件开发的网易新闻页面搭建过程
根据妙味课堂上的一个教程练习总结,供自己复习用 一.功能介绍 一个网易新闻客户端的浏览页面,通过网易新闻的api接口实时获取新闻数据,用vux搭建样式框架,以轮播图,文字滚动,图文列表等形式把内容展示 ...
- 类似掌盟的Tab页 Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签 (转)
原博客地址 :http://blog.csdn.net/xiaanming/article/details/10766053 本文转载,记录学习用,如有需要,请到原作者网站查看(上面这个网址) 之前 ...
- IOS开发--仿制网易新闻
学习来源:袁峥老师的<快速集成App中顶部标题滚动条> 此次博文写的是按需求分析写代码,思路条理性杠杠的,可以提高的编码实现速度哦. 效果: 根据这个网易新闻的界面,需求分析: ...
随机推荐
- linux安全 设置登录失败次数后,拒绝登录
设置登录失败3次后锁定用户300秒可以通过配合文件/etc/pam.d/sshd配置如下 在第一行 #%PAM-1.0 的下一行添加1a auth required pam_tally2.so den ...
- GoLang设计模式16 - 模板方法模式
模板方法设计模式是一种行为型设计模式.这种模式通过常用于为某种特定的操作定义一个模板或者算法模型. 以一次性密码(OTP:One Time Password)为例.我们常见的一次性密码有两种:短信密码 ...
- [atARC127F]±AB
(为了方便,以下除$V$外都改为小写字母) 结论1:若$a+b\le m+1$,则答案为$m+1$(即任意$x$都可以被得到) 任取$y\in [0,m]$,由$\gcd(a,b)=1$存在$y-V= ...
- [hdu7081]Pty loves book
建立ac自动机,令$S_{x}$为以根到$x$的路径所构成的字符串以及$L_{x}=|S_{x}|,W_{x}=\sum_{1\le i\le m,t_{i}为S_{x}的后缀}w_{i}$,那么不难 ...
- [bzoj1391]order
考虑最小割,即最少要去掉多少收益先S向所有机器连边,流量为购买费用:所有机器向工作连边,流量为租借费用:工作向T连边,流量为收益那么对于每一个工作,要么割掉连向T的边,要么购买/租借所有机器,同时由于 ...
- 微信小程序的优点(水一篇)
- 快速的加载 - 更强大的能力 - 原生的体验 - 易用且安全的微信数据开放 - 高效和简单的开发 摘自微信官方文档 https://developers.weixin.qq.com/minipro ...
- Golang - 关于 proto 文件的一点小思考
目录 前言 helloworld.proto 小思考 小结 推荐阅读 前言 ProtoBuf 是什么? ProtoBuf 是一套接口描述语言(IDL),通俗的讲是一种数据表达方式,也可以称为数据交换格 ...
- azkaban执行任务长时间无法结束
问题显示: 由于一次执行较多的任务,导致azkaban的web程序崩溃,此时,关闭azkaban服务,重新启动azkaban 但是由于azkaban的exec程序无法关闭,这里采用kill的方式关掉e ...
- 【R】爬虫案例
爬取豆瓣相册 library(RCurl) library(XML) myHttpheader <- c("User-Agent"="Mozilla/5.0 (Wi ...
- BaiduPCS-Go----百度云下载工具
1.网页登录百度网盘:https://pan.baidu.com/2.百度输入法生成:http://pcs.baidu.com/rest/2.0/pcs/file?app_id=265486& ...