原文:http://blog.csdn.net/hmt20130412/article/details/34523235

本来只是打算介绍一下addChildViewController这个方法的,正好今天朋友去换工作面试问到网易新闻标签栏效果的实现,就结合它,用个小Demo实例介绍一下:(具体解释都写在了Demo里面的注释)

  1. //
  2. //  HMTMainViewController.m
  3. //  UIScrollView
  4. //
  5. //  Created by HMT on 14-6-25.
  6. //  Copyright (c) 2014年 humingtao. All rights reserved.
  7. //
  8. #import "HMTMainViewController.h"
  9. #import "HMTFirstViewController.h"
  10. #import "HMTSecondViewController.h"
  11. #import "HMTThirdViewController.h"
  12. @interface HMTMainViewController () <UIScrollViewDelegate>
  13. @property (nonatomic ,strong) HMTThirdViewController  *thirdVC;
  14. @property (nonatomic ,strong) HMTFirstViewController  *firstVC;
  15. @property (nonatomic ,strong) HMTSecondViewController *secondVC;
  16. @property (nonatomic ,strong) UIViewController *currentVC;
  17. @property (nonatomic ,strong) UIScrollView *headScrollView;  //  顶部滚动视图
  18. @property (nonatomic ,strong) NSArray *headArray;
  19. @end
  20. @implementation HMTMainViewController
  21. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  22. {
  23. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  24. if (self) {
  25. // Custom initialization
  26. }
  27. return self;
  28. }
  29. - (void)viewDidLoad
  30. {
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. self.navigationItem.title = @"网易新闻Demo";
  34. self.headArray = @[@"头条",@"娱乐",@"体育",@"财经",@"科技",@"NBA",@"手机"];
  35. /**
  36. *   automaticallyAdjustsScrollViewInsets   又被这个属性坑了
  37. *   我"UI高级"里面一篇文章着重讲了它,大家可以去看看
  38. */
  39. self.automaticallyAdjustsScrollViewInsets = NO;
  40. self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, 320, 40)];
  41. self.headScrollView.backgroundColor = [UIColor purpleColor];
  42. self.headScrollView.contentSize = CGSizeMake(560, 0);
  43. self.headScrollView.bounces = NO;
  44. self.headScrollView.pagingEnabled = YES;
  45. [self.view addSubview:self.headScrollView];
  46. for (int i = 0; i < [self.headArray count]; i++) {
  47. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  48. button.frame = CGRectMake(0 + i*80, 0, 80, 40);
  49. [button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];
  50. button.tag = i + 100;
  51. [button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  52. [self.headScrollView addSubview:button];
  53. }
  54. /*
  55. 苹 果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用 [self addChildViewController:child]方法将sub view对应的viewController也加到当前 ViewController的管理中。
  56. 对 于那些当前暂时不需要显示的subview,只通过addChildViewController把subViewController加进去;需要显示 时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。
  57. 这样做的好处:
  58. 1.无疑,对页面中的逻辑更加分明了。相应的View对应相应的ViewController。
  59. 2.当某个子View没有显示时,将不会被Load,减少了内存的使用。
  60. 3.当内存紧张时,没有Load的View将被首先释放,优化了程序的内存释放机制。
  61. */
  62. /**
  63. *  在iOS5中,ViewController中新添加了下面几个方法:
  64. *  addChildViewController:
  65. *  removeFromParentViewController
  66. *  transitionFromViewController:toViewController:duration:options:animations:completion:
  67. *  willMoveToParentViewController:
  68. *  didMoveToParentViewController:
  69. */
  70. self.firstVC = [[HMTFirstViewController alloc] init];
  71. [self.firstVC.view setFrame:CGRectMake(0, 104, 320, 464)];
  72. [self addChildViewController:_firstVC];
  73. self.secondVC = [[HMTSecondViewController alloc] init];
  74. [self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)];
  75. self.thirdVC = [[HMTThirdViewController alloc] init];
  76. [self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)];
  77. //  默认,第一个视图(你会发现,全程就这一个用了addSubview)
  78. [self.view addSubview:self.firstVC.view];
  79. self.currentVC = self.firstVC;
  80. }
  81. - (void)didClickHeadButtonAction:(UIButton *)button
  82. {
  83. //  点击处于当前页面的按钮,直接跳出
  84. if ((self.currentVC == self.firstVC && button.tag == 100)||(self.currentVC == self.secondVC && button.tag == 101.)) {
  85. return;
  86. }else{
  87. //  展示2个,其余一样,自行补全噢
  88. switch (button.tag) {
  89. case 100:
  90. [self replaceController:self.currentVC newController:self.firstVC];
  91. break;
  92. case 101:
  93. [self replaceController:self.currentVC newController:self.secondVC];
  94. break;
  95. case 102:
  96. //.......
  97. break;
  98. case 103:
  99. //.......
  100. break;
  101. case 104:
  102. //.......
  103. break;
  104. case 105:
  105. //.......
  106. break;
  107. case 106:
  108. //.......
  109. break;
  110. //.......
  111. default:
  112. break;
  113. }
  114. }
  115. }
  116. //  切换各个标签内容
  117. - (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
  118. {
  119. /**
  120. *            着重介绍一下它
  121. *  transitionFromViewController:toViewController:duration:options:animations:completion:
  122. *  fromViewController      当前显示在父视图控制器中的子视图控制器
  123. *  toViewController        将要显示的姿势图控制器
  124. *  duration                动画时间(这个属性,old friend 了 O(∩_∩)O)
  125. *  options                 动画效果(渐变,从下往上等等,具体查看API)
  126. *  animations              转换过程中得动画
  127. *  completion              转换完成
  128. */
  129. [self addChildViewController:newController];
  130. [self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
  131. if (finished) {
  132. [newController didMoveToParentViewController:self];
  133. [oldController willMoveToParentViewController:nil];
  134. [oldController removeFromParentViewController];
  135. self.currentVC = newController;
  136. }else{
  137. self.currentVC = oldController;
  138. }
  139. }];
  140. }

【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)的更多相关文章

  1. iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

    本文转载至 http://www.tuicool.com/articles/3ymMzub CSDN博客原文  http://blog.csdn.net/hmt20130412/article/det ...

  2. IOS开发--仿制网易新闻

    学习来源:袁峥老师的<快速集成App中顶部标题滚动条> 此次博文写的是按需求分析写代码,思路条理性杠杠的,可以提高的编码实现速度哦. 效果:   根据这个网易新闻的界面,需求分析:     ...

  3. iOS界面-仿网易新闻左侧抽屉式交互 续(添加新闻内容页和评论页手势)

     本文转载至  http://blog.csdn.net/totogo2010/article/details/8637430       1.介绍 有的博友看了上篇博文iOS界面-仿网易新闻左侧抽屉 ...

  4. IOS开发中UITableView(表视图)的滚动优化及自定义Cell

    IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...

  5. iOS开发app启动原理及视图和控制器的函数调用顺序

    main()函数是整个程序的入口,在程序启动之前,系统会调用exec()函数.在Unix中exec和system的不同在于,system是用shell来调用程序,相当于fork+exec+waitpi ...

  6. IOS之分析网易新闻存储数据(CoreData的使用,增删改查)

    用过网易新闻客户端的朋友们都知道,获取新闻列表时有的时候他会请求网络有时候不会,查看某条新闻的时候再返回会标注已经查看的效果,接下来分析一下是如何实现的. 首先: 1.网易新闻用CoreData存储了 ...

  7. iOS学习之网易新闻简易Demo

    简易NewsDemo代码分析 界面布局就不多说了.效果图:(自定义了三套Cell,最后一套Cell是页面最下方的"正在加载中..."Cell,图三.)         主要分析工程 ...

  8. iOS开发之创建颜色渐变视图View

    在iOS开发中有时需要自己自定义一个视图view的背景,而网上有人提出的在循环中不断alloc的方法设置其背景色渐变,会耗费很多内存和资源,极其不明智,而在CALayer中早就提供有图层渐变的类和相应 ...

  9. 基于VUE,VUX组件开发的网易新闻页面搭建过程

    根据妙味课堂上的一个教程练习总结,供自己复习用 一.功能介绍 一个网易新闻客户端的浏览页面,通过网易新闻的api接口实时获取新闻数据,用vux搭建样式框架,以轮播图,文字滚动,图文列表等形式把内容展示 ...

随机推荐

  1. SPSS问题

    1.SPSS中变量视图里的度量标准的三个选项是什么意思 Nominal名义变量是对数据进行分类得到的变量,如按性别分为男女,按年龄分为老.中.青: Ordinal顺序变量是对数据进行排序得到的变量,如 ...

  2. JavaScript----Performance Tool and Process

    1. Syntax-------------JSLint, JSHint, 2. Combine------------YUI 3. Minify---------------JSMin

  3. Yaroslav and Sequence

    Codeforces Round #182 (Div. 1) A:http://codeforces.com/contest/301/problem/A 题意:给你2*n-1个数,你每次可以选择n个连 ...

  4. LINUX HA:Pacemaker + Corosync初配成功

    参考很多文档: http://zhumeng8337797.blog.163.com/blog/static/100768914201218115650522/ 下一步,想想这个PC组和与HAPROX ...

  5. Qt中暂停线程的执行(利用QMutex,超级简单明了)

    在线程中定义一个信号量: QMutex pause;把run()函数中循环执行的部分用信号量pause锁住: void run() { while(1) { pause.lock(); //循环执行的 ...

  6. SQL约束和字段约束的创建和删除

    1)禁止所有表约束的SQLselect 'alter table '+name+' nocheck constraint all' from sysobjects where type='U' 2)删 ...

  7. 《A First Course in Probability》-chaper3-条件概率和独立性-贝叶斯公式、全概率公式

    设有事件A.B. 下面结合具体的题目进一步理解这种方法: Q1:保险公司认为人可以分为两类,一类易出事故,另一类则不易出事故.统计表明,一个易出事故者在一年内发生事故的概率是0.4,而对不易出事故者来 ...

  8. Huffman编码实现电文的转码与译码

    //first thing:thanks to my teacher---chenrong      Dalian Maritime university /* 构造Huffman Tree思路: ( ...

  9. Android注解利器:ButterKnife 的基本使用

    前言 ButterKnife 简介 ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,可视化一 ...

  10. Atom 编辑器 前端基本插件

    Atom 编辑器插件 这个编辑器是github出品,现在处于免费试用期:如果是初学者,可以使用这个编辑器,插件安装很方便,只需要点菜单栏的File-Settings-Install,在搜索框中输入想要 ...