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

转自 http://blog.csdn.net/hmt20130412/article/details/34523235?utm_source=tuicool   还有好多知识 大家可以学习

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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值