zoukankan      html  css  js  c++  java
  • 【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

      原文: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.   
    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 ,strong) HMTSecondViewController *secondVC;  
    19.   
    20. @property (nonatomic ,strong) UIViewController *currentVC;  
    21.   
    22. @property (nonatomic ,strong) UIScrollView *headScrollView;  //  顶部滚动视图  
    23.   
    24. @property (nonatomic ,strong) NSArray *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(0, 64, 320, 40)];  
    53.     self.headScrollView.backgroundColor = [UIColor purpleColor];  
    54.     self.headScrollView.contentSize = CGSizeMake(560, 0);  
    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*80, 0, 80, 40);  
    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(0, 104, 320, 464)];  
    89.     [self addChildViewController:_firstVC];  
    90.       
    91.     self.secondVC = [[HMTSecondViewController alloc] init];  
    92.     [self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)];  
    93.       
    94.     self.thirdVC = [[HMTThirdViewController alloc] init];  
    95.     [self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)];  
    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. }  

     


  • 相关阅读:
    SpringCloud_组件常用注解
    SpringBoot_启动器
    SICP习题 1.5 (应用序与正则序)
    SICP习题 1.4 ( if 语句返回运算符)
    SICP习题 1.3 (求较大两个数的递归)
    SICP习题 1.2 (前缀表达式)
    SICP习题 1.1 (if cond语句)
    MySQL5.7 踩坑实录
    类找不到总结java.lang.ClassNotFoundException
    网易校招2018----题目3----字符串碎片
  • 原文地址:https://www.cnblogs.com/A--G/p/4569831.html
Copyright © 2011-2022 走看看