標籤:i++ current jsb stat cross 科技 class ios開發 ++
本來僅僅是打算介紹一下addChildViewController這種方法的,正好今天朋友去換工作面試問到網易新聞標籤欄效果的實現,就結合它,用個小Demo執行個體介紹一下:(詳細解釋都寫在了Demo裡面的凝視)
//// HMTMainViewController.m// UIScrollView//// Created by HMT on 14-6-25.// Copyright (c) 2014年 humingtao. All rights reserved.//#import "HMTMainViewController.h"#import "HMTFirstViewController.h"#import "HMTSecondViewController.h"#import "HMTThirdViewController.h"@interface HMTMainViewController () <UIScrollViewDelegate>@property (nonatomic ,strong) HMTThirdViewController *thirdVC;@property (nonatomic ,strong) HMTFirstViewController *firstVC;@property (nonatomic ,strong) HMTSecondViewController *secondVC;@property (nonatomic ,strong) UIViewController *currentVC;@property (nonatomic ,strong) UIScrollView *headScrollView; // 頂部滾動視圖@property (nonatomic ,strong) NSArray *headArray;@end@implementation HMTMainViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"網易新聞Demo"; self.headArray = @[@"頭條",@"娛樂",@"體育",@"財經",@"科技",@"NBA",@"手機"]; /** * automaticallyAdjustsScrollViewInsets 又被這個屬性坑了 * 我"UI進階"裡面一篇文章著重講了它,大家能夠去看看 */ self.automaticallyAdjustsScrollViewInsets = NO; self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, 320, 40)]; self.headScrollView.backgroundColor = [UIColor purpleColor]; self.headScrollView.contentSize = CGSizeMake(560, 0); self.headScrollView.bounces = NO; self.headScrollView.pagingEnabled = YES; [self.view addSubview:self.headScrollView]; for (int i = 0; i < [self.headArray count]; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(0 + i*80, 0, 80, 40); [button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal]; button.tag = i + 100; [button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.headScrollView addSubview:button]; } /* 蘋果新的API添加了addChildViewController方法,而且希望我們在使用addSubview時,同一時候調用[self addChildViewController:child]方法將sub view相應的viewController也加到當前ViewController的管理中。 對於那些當前臨時不須要顯示的subview,僅僅通過addChildViewController把subViewController加進去;須要顯示時再調用transitionFromViewController方法。將其加入進入底層的ViewController中。 這樣做的優點: 1.無疑,對頁面中的邏輯更加分明了。相應的View相應相應的ViewController。
2.當某個子View沒有顯示時,將不會被Load,降低了記憶體的使用。 3.當記憶體緊張時,沒有Load的View將被首先釋放,最佳化了程式的記憶體釋放機制。 */ /** * 在iOS5中。ViewController中新加入了以下幾個方法: * addChildViewController: * removeFromParentViewController * transitionFromViewController:toViewController:duration:options:animations:completion: * willMoveToParentViewController: * didMoveToParentViewController: */ self.firstVC = [[HMTFirstViewController alloc] init]; [self.firstVC.view setFrame:CGRectMake(0, 104, 320, 464)]; [self addChildViewController:_firstVC]; self.secondVC = [[HMTSecondViewController alloc] init]; [self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)]; self.thirdVC = [[HMTThirdViewController alloc] init]; [self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)]; // 預設,第一個視圖(你會發現,全程就這一個用了addSubview) [self.view addSubview:self.firstVC.view]; self.currentVC = self.firstVC; }- (void)didClickHeadButtonAction:(UIButton *)button{ // 點擊處於當前頁面的按鈕,直接跳出 if ((self.currentVC == self.firstVC && button.tag == 100)||(self.currentVC == self.secondVC && button.tag == 101.)) { return; }else{ // 展示2個,其餘一樣,自行補全噢 switch (button.tag) { case 100: [self replaceController:self.currentVC newController:self.firstVC]; break; case 101: [self replaceController:self.currentVC newController:self.secondVC]; break; case 102: //....... break; case 103: //....... break; case 104: //....... break; case 105: //....... break; case 106: //....... break; //....... default: break; } }}// 切換各個標籤內容- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController{ /** * 著重介紹一下它 * transitionFromViewController:toViewController:duration:options:animations:completion: * fromViewController 當前顯示在父視圖控制器中的子視圖控制器 * toViewController 將要顯示的姿勢圖控制器 * duration 動畫時間(這個屬性,old friend 了 O(∩_∩)O) * options 動畫效果(漸層,從下往上等等,詳細查看API) * animations 轉換過程中得動畫 * completion 轉換完畢 */ [self addChildViewController:newController]; [self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) { if (finished) { [newController didMoveToParentViewController:self]; [oldController willMoveToParentViewController:nil]; [oldController removeFromParentViewController]; self.currentVC = newController; }else{ self.currentVC = oldController; } }];}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG10MjAxMzA0MTI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >
iOS開發 剖析網易新聞標籤欄視圖切換(addChildViewController屬性介紹)