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: (Detailed explanation are written in the demo inside the gaze)
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", @ "wealth", @ "technology", @ "NBA", @ "mobile"]; /** * Automaticallyadjustsscrollviewinsets again by this attribute hole * I "UI advanced" inside an article focused on it, we 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 we want to call the [self Addchildviewcontroller:child] method at the same time when using Addsubview to set the Sub VI EW 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 the Subviewcontroller, need to display the Transitionfromviewcontroller method is called.Add it to the bottom of the viewcontroller. The advantages of this: 1. Undoubtedly, the logic in the page is more distinct. Corresponding view corresponding 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. Viewcontroller added the following methods: * Addchildviewcontroller: * Removefromparentviewcontroller * TRANSITIONFROMVIEWC Ontroller:toViewController:duration:options:animations:completion: * Willmovetoparentviewcontroller: * Didmoveto Parentviewcontroller: */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, just jump out 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:optio Ns:animations:completion: * Fromviewcontroller The Child View controller currently displayed in the parent view controller * Toviewcontroller the pose diagram controller that will be displayed * Duration Animation Time (this property, old friend O (∩_∩) o) * Options animation effect (gradient, from bottom to top and so on, Detailed view API) * Animations conversion Process Animation * Completion conversion complete */[self Addchildviewcontroller:newcontro Ller]; [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; } }];}
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvag10mjaxmza0mti=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
iOS Development Anatomy NetEase News tab bar View Toggle (Addchildviewcontroller Property Introduction)