Originally just intended to introduce addchildviewcontroller this method, just today friends to change job interview asked NetEase news tab effect Implementation, combine it, with a small demo example introduction: (The specific explanation are written in the demo inside the note)
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; Top scrolling View @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 to self;} -(void) viewdidload{[Super ViewdiDload]; Do any additional setup after loading the view. Self.navigationItem.title = @ "NetEase news demo"; Self.headarray = @[@ "headline", @ "Entertainment", @ "Sports", @ "Finance", @ "technology", @ "NBA", @ "mobile"]; /** * Automaticallyadjustsscrollviewinsets again by this attribute hole * I "UI advanced" inside an article focused on it, you can go to see * * * self.automaticall Yadjustsscrollviewinsets = 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:uibuttont Ypesystem]; Button.frame = CGRectMake (0 + i*80, 0, 80, 40); [Button Settitle:[self.headarray objectatindex:i] forstate:uicontrolstatenormal]; Button.tag = i + 100; [ButtonAddtarget:self Action: @selector (didclickheadbuttonaction:) forcontrolevents:uicontroleventtouchupinside]; [Self.headscrollview Addsubview:button]; }/* The new API adds the Addchildviewcontroller method and wants us to call the [self Addchildviewcontroller:child] method while using Addsubview to set the sub view The corresponding Viewcontroller is also added to the current Viewcontroller management. For those who currently do not need to display the Subview, only through the Addchildviewcontroller to add Subviewcontroller, need to be displayed and then call the Transitionfromviewcontroller method. Add it to the underlying viewcontroller. The benefits of doing this: 1. Undoubtedly, the logic in the page is more distinct. The corresponding view corresponds to the corresponding viewcontroller. 2. When a sub view is not displayed, it will not be load, reducing the use of memory. 3. When memory is tight, the view without load is released first, optimizing the program's memory release mechanism. *//** * in IOS5, the following new methods are added in Viewcontroller: * Addchildviewcontroller: * Removefromparentviewcontrolle R * TransitionFromViewController:toViewController:duration:options:animations:completion: * Willmovetoparentview Controller: * 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)]; By default, the first view (you will find that this one is used Addsubview) [Self.view AddSubview:self.firstVC.view]; SELF.CURRENTVC = SELF.FIRSTVC; }-(void) Didclickheadbuttonaction: (UIButton *) button{//Click the button on the current page to jump directly out of if (SELF.CURRENTVC = SELF.FIRSTVC & & button.tag = = 100) | | (SELF.CURRENTVC = = SELF.SECONDVC && Button.tag = = 101.)) {return; }else{//Show 2, the rest, self-complement OH switch (Button.tag) {case: [Self Replacecontro Ller: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://... break; Case 106://... break; ..... default:break; }}}//Toggle Individual label Contents-(void) Replacecontroller: (Uiviewcontroller *) Oldcontroller Newcontroller: (Uiviewcontroller *) newcontroller{/** * Highlights It * TransitionFromViewController:toViewController:duration:options:anima Tions:completion: * Fromviewcontroller The Child View controller currently displayed in the parent view Controller * Toviewcontroller The Posture Graph controller that will be displayed * dur ation Animation Time (this property, old friend O (∩_∩) o) * Options animation effect (gradient, from bottom to top, etc., see API) * Animati During the ONS conversion process the animation * Completion conversion completed */[self AddchildvieWcontroller:newcontroller]; [Self Transitionfromviewcontroller:oldcontroller toviewcontroller:newcontroller duration:2.0 options: Uiviewanimationoptiontransitioncrossdissolve Animations:nil completion:^ (BOOL finished) {if (Finis hed) {[Newcontroller didmovetoparentviewcontroller:self]; [Oldcontroller Willmovetoparentviewcontroller:nil]; [Oldcontroller Removefromparentviewcontroller]; SELF.CURRENTVC = Newcontroller; }else{SELF.CURRENTVC = Oldcontroller; } }];}