iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
? ? ? ? ? ? 本來僅僅是打算介紹一下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; } }]; }
轉載于:https://www.cnblogs.com/mthoutai/p/7040531.html
總結
以上是生活随笔為你收集整理的iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#文件流上传图片
- 下一篇: JSON Web Tokens(JWT)