iOS編程開發中pageController介面開發教程,iospagecontroller

來源:互聯網
上載者:User

iOS編程開發中pageController介面開發教程,iospagecontroller

iOS編程開發中pageController介面開發教程,如下:

項目調用順序:

1. 初始化標題scrollView --> 2.初始化內容scrollView --> 3. 添加所有子控制器 setupAllChildViewController

--> 4. 設定所有標題 setupTitle --> 5. 標題點擊方法 titleChilk: --> 6. 點擊標題時,改變標題顏色 switchColor:

--> 7. 設定標題選中置中 setupTitleCenter: --> 8. 點擊標題,將對應的子控制器View添加到內容scrollView上 addOneViewController:??--> 9. 設定代理,監聽內容scrollView滾動 --> 10. 調用 滾動完成的時候 和 滾動時的方法

ZYViewController檔案 : ViewController.

//  Created by 朝陽 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 {    // 調用父類的viewDidLoad    [super viewDidLoad];    // 添加所有子控制器    [self setupAllChildViewController];    // 當調用玩viewDidLoad後,就會調用viewWillAppear:方法,此類沒有該方法,就會去父類找}#pragma -mark 添加所有的子控制器 3- (void)setupAllChildViewController{    //頭條    HeadLineViewController *headLineVC = [[HeadLineViewController alloc] init];    headLineVC.title = @"頭條";    /*     在這裡設定背景顏色不好,因為當程式運行後,所有的控制器中的內容都載入了出來.     應該點擊對應的標題按鈕後再去載入,因此把顏色設定在對應控制器的viewDidload裡     */    // headLineVC.view.backgroundColor = [UIColor blueColor];    [self addChildViewController:headLineVC];    //熱點    HotViewController *hotVC = [[HotViewController alloc] init];    hotVC.title = @"熱點";    [self addChildViewController:hotVC];    //視頻    VideoViewController *videoVC = [[VideoViewController alloc] init];    videoVC.title = @"視頻";    [self addChildViewController:videoVC];    //社會    SocialViewController *socialVC = [[SocialViewController alloc] init];    socialVC.title = @"社會";    [self addChildViewController:socialVC];    //訂閱    SubscribeViewController *subscribeVC = [[SubscribeViewController alloc] init];    subscribeVC.title = @"訂閱";    [self addChildViewController:subscribeVC];    //科技    ScienceViewController *scienceVC = [[ScienceViewController alloc] init];    scienceVC.title = @"科技";    [self addChildViewController:scienceVC];        ScienceViewController *scienceVC1 = [[ScienceViewController alloc] init];    scienceVC1.title = @"朝陽";    [self addChildViewController:scienceVC1];    }@end

ViewController檔案

//  Created by 朝陽 on 2018/2/5.//  Copyright ? 2018年 sunny. All rights reserved.//#import "ViewController.h"#define ScreenW [UIScreen mainScreen].bounds.size.width@interface ViewController ()@property (nonatomic, weak) UIScrollView *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 array];        _btnArray = btnArray;    }    return _btnArray;}#pragma -mark 設定所有標題 4- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    //設定標題. 判斷只設定一次    if (_isInitialize == NO) {                [self setupTitle];        _isInitialize = YES;    }    }- (void)viewDidLoad {    [super viewDidLoad];        self.navigationItem.title = @"朝陽新聞";        //1. 建立標題scrollView    [self setupTitleScrollView];    //2. 建立內容scrollView    [self setupContentScrollView];    //4. 設定標題    //5. 標題點擊    //6. 處理內容滾動,視圖滾動(監聽scrollView的滾動)    //7. 選中標題置中    //8. 標題縮放及色彩坡形    }#pragma -mark UIScrollView delegate Method 9// 滾動完成的時候調用- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    // 根據滾動的位移量擷取當前角標    NSInteger i = scrollView.contentOffset.x / ScreenW;    // 根據角標擷取button    // UIButton *button = self.titleScrollView.subviews[i]; // 可能取出的子控制項紊亂,scrollView內有系統子控制項    UIButton *button = self.btnArray[i];    //1.切換標題顏色    [self switchColor:button];    //2.把對應的子控制器的view添加上去    [self addOneViewController:i];    }// 滾動的時候調用- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    //① 字型縮放 1.縮放比例 2.縮放哪兩個按鈕    //根據位移量來擷取按鈕的角標    NSInteger leftI = scrollView.contentOffset.x / ScreenW;    NSInteger rightI = leftI + 1;    //擷取左邊的按鈕    UIButton *leftBtn = self.btnArray[leftI];    //擷取右邊的按鈕    //防止越界    UIButton *rightBtn;    if (rightI < self.btnArray.count) {        rightBtn = self.btnArray[rightI];    }        // 縮放比例 0 ~ 1 => 1 ~ 1.3 (最大為1.3)    CGFloat scaleR = scrollView.contentOffset.x / ScreenW;    // 始終保持縮放比例為0 ~ 1    scaleR -= leftI;    //NSLog(@"%f",scaleR * 0.3 + 1);    CGFloat scaleL = 1 - scaleR;    //NSLog(@"%f",scaleL);        //縮放按鈕    leftBtn.transform = CGAffineTransformMakeScale(scaleL * 0.3 + 1, scaleL * 0.3 + 1);    rightBtn.transform = CGAffineTransformMakeScale(scaleR * 0.3 + 1, scaleR * 0.3 + 1);        //② 色彩坡形    UIColor *rightColor = [UIColor colorWithRed:scaleR green:0 blue:0 alpha:1];    UIColor *leftColor = [UIColor colorWithRed:scaleL green:0 blue:0 alpha:1];    [rightBtn setTitleColor:rightColor forState:UIControlStateNormal];    [leftBtn setTitleColor:leftColor forState:UIControlStateNormal];        /*        白色 1 1 1        黑色 0 0 0        紅色 1 0 0     */}#pragma -mark 標題置中 7- (void)setupTitleCenter:(UIButton *)button{    //標題置中本質:是標題按鈕的位移量    CGFloat offsetX = button.center.x - ScreenW * 0.5;    // NSLog(@"%f",offsetX);    if (offsetX < 0) {        offsetX = 0;    }    //標題按鈕最大的位移量    CGFloat maxOffsetX = self.titleScrollView.contentSize.width - ScreenW;    // 如果位移量 > 最大位移量    if (offsetX > maxOffsetX) {        offsetX = maxOffsetX;    }        [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];}#pragma -mark 點擊標題切換顏色 6- (void)switchColor:(UIButton *)button{    self.selectBtn.transform = CGAffineTransformIdentity;    [self.selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    // 設定標題選中置中    [self setupTitleCenter:button];    // 設定標題文字縮放    button.transform = CGAffineTransformMakeScale(1.3, 1.3);    self.selectBtn = button;}#pragma -mark 添加一個子控制器view 8static CGFloat x;- (void)addOneViewController:(NSInteger)i;{        UIViewController *vc = self.childViewControllers[i];    // 如果此時view的控制器已經添加到contentScrollView上,不再重複添加    //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 設定標題點擊 5- (void)titleClick:(UIButton *)button{    NSInteger i = button.tag;    //1.點擊標題時,標題顏色變為紅色    [self switchColor:button];    //2.點擊某個標題,將對應子控制器的view添加上去    [self addOneViewController:i];    //3.內容滾動視圖滾動到對應的位置(設定位移量)    self.contentScrollView.contentOffset = CGPointMake(x, 0);}#pragma -mark 設定所有標題- (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) {        //建立按鈕        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];        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];        // 設定預設選擇第一個標題        if (i == 0) {            [self titleClick:btn];        }        [self.btnArray addObject:btn];        [self.titleScrollView addSubview:btn];    }    // 設定標題scrollView滾動範圍    self.titleScrollView.contentSize = CGSizeMake(count * btnW, 0);    self.titleScrollView.showsHorizontalScrollIndicator = NO;    // 設定內容的滾動範圍    self.contentScrollView.contentSize = CGSizeMake(count * ScreenW, 0);}#pragma -mark 初始化標題捲軸 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 初始化內容捲軸 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.