原文: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. }

最新文章

  1. SCNU ACM 2016新生赛决赛 解题报告
  2. plist文件、NSUserDefault 对文件进行存储的类、json格式解析
  3. C语言 &#183; 送分啦
  4. Docker简明教程(转)
  5. mac 远程连接服务器
  6. Flash与JS之间相互调用以及参数传递
  7. Objective-C入门
  8. 微信的User-Agent
  9. debian attempt to kill init!
  10. ruby中symbol
  11. string的操作
  12. PHP Simple HTML DOM 使用
  13. 加载为应用程序池‘DefaultAppPool&#39;提供服务的进程失败,应用程序池被禁用【解决方法】
  14. Java NIO与IO
  15. HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)
  16. webstorm 卡死解决方法
  17. Hibernate(五):Hibernate配置文件及C3P0的用法
  18. 用js来实现那些数据结构13(树01-二叉搜索树的实现)
  19. threejs学习笔记(二)
  20. linux 服务器下的基本操作

热门文章

  1. Firebase能改变什么(对SaaS,BaaS,PaaS,IaaS的解释比较清楚)
  2. 深入浅出 Java Concurrency (1) : J.U.C的整体认识
  3. java学习面向对象之设计模式之单例模式
  4. Python import / pyd / dll
  5. 数学概念——D 期望
  6. Ubuntu中Nginx的安装与配置
  7. Vagrant虚拟机的配置管理
  8. Agri-Net(prim算法,最小生成树问题)
  9. tessilstrona
  10. C#中的 IList, ICollection ,IEnumerable 和 IEnumerator