PageController interface development tutorial in iOS programming development, iospagecontroller

Source: Internet
Author: User

PageController interface development tutorial in iOS programming development, iospagecontroller

In iOS Programming Development, The pageController interface development tutorial is as follows:

Project call sequence:

1. initialize the title scrollView --> 2. initialize the content scrollView --> 3. Add all sub-controllers setupAllChildViewController

--> 4. Set all titles setupTitle --> 5. Title clicking method titleChilk: --> 6. When you click the title, change the title Color switchColor:

--> 7. Set the title to select the center setupTitleCenter: --> 8. Click the title to add the corresponding sub-controller View to the scrollView. addOneViewController :?? --> 9. Set the proxy and listen for scrollView scrolling --> 10. Call the method when the scrolling is complete and when the scrolling is complete.

ZYViewController file: ViewController.

// Created by Chaoyang on 2018/2/6. // Copyright? 2018 sunny. all rights reserved. // # import "ZYViewController. h "# import" HeadLineViewController. h "# import" HotViewController. h "# import" VideoViewController. h "# import" SocialViewController. h "# import" SubscribeViewController. h "# import" ScienceViewController. h "@ interface ZYViewController () @ end @ implementation ZYViewController-(void) viewDidLoad {// call the viewDidLoad [super viewDidLoad] of the parent class; // Add all the sub-controllers [self setupAllChildViewController]; // after you call viewDidLoad, The viewWillAppear: method is called. This method does not exist, it will go to the parent class to find} # pragma-mark to add all the sub-controllers 3-(void) setupAllChildViewController {// toutiaoheadlineviewcontroller * headLineVC = [[HeadLineViewController alloc] init]; headLineVC. title = @ "toutiao.com";/* it is difficult to set the background color here, because when the program runs, all the contents in the controller are loaded. you should click the corresponding title button and then load it. Therefore, set the color in the viewDidload of the corresponding controller * // headLineVC. view. backgroundColor = [UIColor blueColor]; [self addChildViewController: headLineVC]; // hotspot HotViewController * hotVC = [[HotViewController alloc] init]; hotVC. title = @ "Hotspot"; [self addChildViewController: hotVC]; // video VideoViewController * videoVC = [[VideoViewController alloc] init]; videoVC. title = @ "video"; [self addChildViewController: videoVC]; // * alviewcontroller * socialVC = [[SocialViewController alloc] init]; socialVC. title = @ ""; [self addChildViewController: socialVC]; // SubscribeViewController * subscribeVC = [[SubscribeViewController alloc] init]; subscribeVC. title = @ "subscribe"; [self addChildViewController: subscribeVC]; // technology ScienceViewController * scienceVC = [[ScienceViewController alloc] init]; scienceVC. title = @ "technology"; [self addChildViewController: scienceVC]; ScienceViewController * scienceVC1 = [[ScienceViewController alloc] init]; scienceVC1.title = @ "Chaoyang"; [self addChildViewController: scienceVC1];} @ end

ViewController File

// Created by Chaoyang on 2018/2/5. // Copyright? Sunny. All rights reserved. // # import "ViewController. h" # define ScreenW [UIScreen mainScreen]. bounds. size. width @ interface ViewController ()
 
  
@ Property (nonatomic, weak) implements * titleScrollView; @ property (nonatomic, weak) UIScrollView * contentScrollView; @ property (nonatomic, weak) UIButton * selectBtn; @ property (nonatomic, strong) NSMutableArray * btnArray; @ property (nonatomic, assign) BOOL isInitialize; @ end @ implementation ViewController-(NSMutableArray *) btnArray {if (_ btnArray = nil) {NSMutableArray * btnArray = [NSMutableArray Rray]; _ btnArray = btnArray;} return _ btnArray;} # pragma-mark set all titles 4-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; // set the title. set if (_ isInitialize = NO) {[self setupTitle]; _ isInitialize = YES ;}}-(void) viewDidLoad {[super viewDidLoad]; self. navigationItem. title = @ "Chaoyang News"; // 1. create the title scrollView [self setupTitleScrollView]; // 2. create content scrollView [self setupContentScro LlView]; // 4. set the title // 5. click the title // 6. process content scrolling and view scrolling (Listening to scrollView scrolling) // 7. select center title // 8. title scaling and color gradient} # pragma-mark UIScrollView delegate Method 9 // call-(void) scrollViewDidEndDecelerating (UIScrollView *) when rolling is complete *) scrollView {// obtain the current badge NSInteger I = scrollView Based on the scrolling offset. contentOffset. x/ScreenW; // obtain the button Based on the badge // UIButton * button = self. titleScrollView. subviews [I]; // sub-controls that may be retrieved are disordered, and UIButton * butt is a sub-control in scrollView. On = self. btnArray [I]; // 1. switch the title Color [self switchColor: button]; // 2. add the view of the corresponding sub-controller to [self addOneViewController: I];} // call-(void) scrollViewDidScroll (UIScrollView *) When scrolling *) scrollView {// ① font scaling 1. scale ratio 2. which two buttons are scaled? // obtain the NSInteger leftI = scrollView. contentOffset. x/ScreenW; NSInteger rightI = leftI + 1; // obtain the left button UIButton * leftBtn = self. btnArray [leftI]; // obtain the button on the right. // prevents cross-border UIButton * rightBtn; if (RightI <self. btnArray. count) {rightBtn = self. btnArray [rightI];} // The scaling ratio is 0 ~ 1 => 1 ~ 1.3 (maximum: 1.3) CGFloat scaleR = scrollView. contentOffset. x/ScreenW; // always keep the zoom ratio 0 ~ 1 scaleR-= leftI; // NSLog (@ "% f", scaleR * 0.3 + 1); CGFloat scaleL = 1-scaleR; // NSLog (@ "% f ", scaleL); // The zoom button leftBtn. transform = CGAffineTransformMakeScale (scaleL * 0.3 + 1, scaleL * 0.3 + 1); rightBtn. transform = CGAffineTransformMakeScale (scaleR * 0.3 + 1, scaleR * 0.3 + 1); // ② color gradient UIColor * rightColor = [UIColor colorWithRed: scaleR green: 0 blue: 0 alpha: 1]; UIColor * leftColor = [UIColor color WithRed: scaleL green: 0 blue: 0 alpha: 1]; [rightBtn setTitleColor: rightColor forState: UIControlStateNormal]; [leftBtn setTitleColor: leftColor forState: UIControlStateNormal]; /* white 1 1 black 0 0 0 Red 1 0 0 */} # pragma-mark title Center 7-(void) setupTitleCenter :( UIButton *) button {// subject center nature: it is the offset of the title button CGFloat offsetX = button. center. x-ScreenW * 0.5; // NSLog (@ "% f", offsetX); if (offsetX <0) {offsetX = 0 ;}/ /The maximum offset of the title button CGFloat maxOffsetX = self. titleScrollView. contentSize. width-ScreenW; // if the Offset> maximum offset if (offsetX> maxOffsetX) {offsetX = maxOffsetX;} [self. titleScrollView setContentOffset: CGPointMake (offsetX, 0) animated: YES] ;}# pragma-mark click the title to change the color 6-(void) switchColor :( UIButton *) button {self. selectBtn. transform = CGAffineTransformIdentity; [self. selectBtn setTitleColor: [UIColor blackColor] f OrState: UIControlStateNormal]; [button setTitleColor: [UIColor redColor] forState: UIControlStateNormal]; // set the title to center [self setupTitleCenter: button]; // set the title text zoom button. transform = CGAffineTransformMakeScale (1.3, 1.3); self. selectBtn = button;} # pragma-mark add a sub-controller view 8 static CGFloat x;-(void) addOneViewController :( NSInteger) I; {UIViewController * vc = self. childViewControllers [I]; // if the Controller of the view has Added to contentScrollView, and no longer added // if (vc. viewIfLoaded) {if (vc. view. superview) {return;} x = I * ScreenW; vc. view. frame = CGRectMake (x, 0, ScreenW, self. contentScrollView. bounds. size. height); [self. contentScrollView addSubview: vc. view] ;}# pragma-mark set title click 5-(void) titleClick :( UIButton *) button {NSInteger I = button. tag; // 1. when you click the title, the title color changes to red [self switchColor: button]; // 2. click a title to add the view of the corresponding sub-controller to [se Lf addOneViewController: I]; // 3. scroll the content View to the corresponding position (Set offset) self. contentScrollView. contentOffset = CGPointMake (x, 0) ;}# pragma-mark set all titles-(void) setupTitle {NSInteger count = self. childViewControllers. count; CGFloat x = 0; CGFloat y = 0; CGFloat btnW = 100; CGFloat btnH = self. titleScrollView. bounds. size. height; for (NSInteger I = 0; I <count; ++ I) {// create button UIButton * btn = [UIButton buttonWithType: UIB UttonTypeCustom]; btn. tag = I; UIViewController * vc = self. childViewControllers [I]; [btn setTitle: vc. title forState: UIControlStateNormal]; x = I * btnW; btn. frame = CGRectMake (x, y, btnW, btnH); [btn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; [btn addTarget: self action: @ selector (titleClick :) forControlEvents: UIControlEventTouchUpInside]; // you can specify the first title by default. if (I = 0) {[self t ItleClick: btn];} [self. btnArray addObject: btn]; [self. titleScrollView addSubview: btn];} // sets the title scrollView rolling range self. titleScrollView. contentSize = CGSizeMake (count * btnW, 0); self. titleScrollView. showsHorizontalScrollIndicator = NO; // you can specify the scroll range of the content. contentScrollView. contentSize = CGSizeMake (count * ScreenW, 0);} # pragma-mark initialize the title scroll bar 1-(void) setupTitleScrollView {UIScrollView * titleScrollView = [UIScrollView alloc] init]; // titleScrollView. backgroundColor = [UIColor purpleColor]; CGFloat y = self. navigationController. navigationBarHidden? 20: 64; titleScrollView. frame = CGRectMake (0, y, self. view. bounds. size. width, 44); [self. view addSubview: titleScrollView]; self. titleScrollView = titleScrollView;} # pragma-mark initialize content scroll bar 2-(void) setupContentScrollView {UIScrollView * contentScrollView = [[UIScrollView alloc] init]; // contentScrollView. backgroundColor = [UIColor grayColor]; CGFloat y = CGRectGetMaxY (self. titleScrollView. frame); contentScrollView. frame = CGRectMake (0, y, self. view. bounds. size. width, self. view. bounds. size. height-y); [self. view addSubview: contentScrollView]; self. contentScrollView = contentScrollView; self. contentScrollView. pagingEnabled = YES; self. contentScrollView. bounces = NO; self. contentScrollView. showsHorizontalScrollIndicator = NO; self. contentScrollView. delegate = self;} @ end
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.