本来只是打算介绍一下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. }

转:addChildViewController实现网易新闻首页切换的更多相关文章

  1. 网易新闻首页iOS

    // //  ViewController.m //  wyy // //  Copyright © 2016年 zm. All rights reserved. // #import "V ...

  2. IOS 类似于网易新闻首页新闻轮播的组件

    一.需求分析 1.可横向循环滚动新闻图片 2.滚动到对应图片时显示新闻标题 3.每张新闻图片可点击 4.有pageControl提示 5.具有控件的扩展能力 二.设计实现 1.显示图片使用SDWebI ...

  3. android 仿网易新闻首页框架

       实现思路很简单左侧栏目是一个一个的 Fragment 的,点击时动态替换各个 Fragment 到当前 Activity 中. 关键代码: public void loadFragment(Ma ...

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

    原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...

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

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

  6. ActionBar+DrawerLayout实现网易新闻客户端首页

    一.概述 随着android版本的不断的更新,google推出了越来越多的高级组件,采用这些官方组件我们可以方便的实现一些以前需要通过复杂编码或者使用第三方组件才能实现的效果,比如slidingmen ...

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

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

  8. 类似掌盟的Tab页 Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签 (转)

    原博客地址  :http://blog.csdn.net/xiaanming/article/details/10766053 本文转载,记录学习用,如有需要,请到原作者网站查看(上面这个网址) 之前 ...

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

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

随机推荐

  1. [cf461E]Appleman and a Game

    考虑我的每一次添加操作,要满足:1.该串是t的子串:2.该串不能与下一次的串开头字母构成t的子串.那么,设f[i][j][k]表示拼i次,第i次填入的开头字母是j,第i+1填入的开头字母是k的最短长度 ...

  2. gantt甘特图可拖拽、编辑(vue、react都可用 highcharts)

    前言   Excel功能强大,应用广泛.随着web应用的兴起和完善,用户的要求也越来越高.很多Excel的功能都搬到了sass里面.恨不得给他们做个Excel出来...程序员太难了... 去年我遇到了 ...

  3. 阿里性能专家全方位对比Jmeter和Locust,到底谁更香?

    近些年,随着互联网行业的不断发展,用户规模也有了爆发性的增长.产品的性能成为影响用户体验的重要因素.因此,性能测试越来越受到大型互联网企业的重视. 在做性能测试时,通常都会借助一些压测工具来模拟大量的 ...

  4. CF1555F Good Graph

    有以下引理: 不存在两个合法环,他们存在公共边. 证明:公共边边权为 \(z\),第一个环除去公共边为 \(x\),第二个环除去公共边为 \(y\). 则有 \(x \oplus z = 1\) \( ...

  5. Atcoder Grand Contest 024 E - Sequence Growing Hard(dp+思维)

    题目传送门 典型的 Atcoder 风格的计数 dp. 题目可以转化为每次在序列中插入一个 \([1,k]\) 的数,共操作 \(n\) 次,满足后一个序列的字典序严格大于前一个序列,问有多少种操作序 ...

  6. 【R绘图】R 基础(base )低级函数legend绘图?

    ggplot虽然好用,但base才是真正的瑞士军刀,什么都能用,各种自定义图形自由组合,出版级图片用base才是王道.但要达到随心所欲,需要熟练掌握. legend是比较重要的低级函数,有很多细节处理 ...

  7. 【MetDNA】基于代谢反应网络的大规模代谢物结构鉴定新算法

    代谢是生命体内化学反应的总称,其所包含的代谢物变化规律可直接反映生命体的健康状态.非靶向代谢组学(untargeted metabolomics)可以在系统水平测量生命体内生理或病理状态下所有代谢物的 ...

  8. PDFium 渲染

    PDFium 是 Chromium 的 PDF 渲染引擎,许可协议为 BSD 3-Clause.不同于 Mozilla 基于 HTML5 的 PDF.js,PDFium 是基于 Foxit Softw ...

  9. java运行报错 has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

    解决方法: 解决办法: 在项目的属性里设置jdk版本,方法是右击项目-->properties-->java compiler --> Enable project specific ...

  10. college-ruled notebook

    TBBT.s3.e10: Sheldon: Where's your notebook?Penny: Um, I don't have one.Sheldon: How are you going t ...