麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > iOS > 正文

iOS多控制器實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫

2019-10-21 18:40:52
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了iOS多控制器實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下

主控制器 ,管理控制器 .h文件

//宏#define kScreenWidth [UIScreen mainScreen].bounds.size.width#define kScreenHeight [UIScreen mainScreen].bounds.size.height#import "MYMainViewController.h"#import "MYFirstViewController.h"#import "MYSecondViewController.h"#import "MYThirdViewController.h"@interface MYMainViewController ()<UIScrollViewDelegate>//控制器名@property (nonatomic, strong) NSArray *VcNames;//選擇欄@property(nonatomic, strong) UIView *clickBar;//底部容器scrollView@property (strong, nonatomic) UIScrollView *containerScrollerView;@end

. m 文件 

底部scrollView , 用于滑動(dòng)

@implementation MYMainViewController- (UIScrollView *)containerScrollerView{  if (!_containerScrollerView) {    _containerScrollerView = [[UIScrollView alloc]init];    _containerScrollerView.pagingEnabled = YES;    _containerScrollerView.showsVerticalScrollIndicator = NO;    _containerScrollerView.showsHorizontalScrollIndicator = NO;    _containerScrollerView.contentSize = CGSizeMake(kScreenWidth *self.VcNames.count,kScreenHeight);    _containerScrollerView.backgroundColor = [UIColor whiteColor];    _containerScrollerView.delegate = self;  }  return _containerScrollerView;}

初始化頂部選擇欄

//三個(gè)子控制器- (NSArray *)VcNames{  if (!_VcNames) {    _VcNames = @[@"控制器一",@"控制器二",@"控制器三"];  }  return _VcNames;}//點(diǎn)擊選擇欄- (UIView *)clickBar{  if (!_clickBar) {    _clickBar = [[UIView alloc]init];    _clickBar.backgroundColor = [UIColor lightGrayColor];    CGFloat width = kScreenWidth / 3;    CGFloat height = 44;    //初始化按鈕    for (NSInteger index = 0; index < 3; index++) {      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];      [button setTitle:self.VcNames[index] forState:UIControlStateNormal];      [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];      button.frame = (CGRect){width *index,0,width,height};      [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];      //綁定tag值      button.tag = index;      [_clickBar addSubview:button];    }  }  return _clickBar;}

viewDidLoad

- (void)viewDidLoad {  [super viewDidLoad];  self.edgesForExtendedLayout = 0;  //初始化選擇欄  [self initClickBar];  //初始化底部scrollView容器  [self initScrollViewContainer];  //初始化子控制器  [self addChildControllers];}

添加子控制器 , 初始化UI

//按鈕選擇欄- (void)initClickBar{  [self.view addSubview:self.clickBar];  self.clickBar.frame = (CGRect){0,0,[UIScreen mainScreen].bounds.size.width,44};}//初始化滑動(dòng)容器- (void)initScrollViewContainer{  [self.view addSubview:self.containerScrollerView];  self.containerScrollerView.frame = CGRectMake(0,44,kScreenWidth, kScreenHeight );}//添加子控制器- (void)addChildControllers{  //為了方便直觀 , 在此處設(shè)置背景色 (實(shí)際開發(fā)中,不能在這里設(shè)置 , 原因是這里只要調(diào)用到了控制器的view屬性 , 該控制器將會(huì)執(zhí)行viewDidLoad方法 , 相當(dāng)于直接一開始就將三個(gè)控制器的所有UI和網(wǎng)絡(luò)請(qǐng)求全加載完了 , 負(fù)荷會(huì)相當(dāng)重)  MYFirstViewController *firstVc = [[MYFirstViewController alloc]init];  firstVc.view.backgroundColor = [UIColor redColor];  [self addChildViewController:firstVc];  MYSecondViewController *secondVc = [[MYSecondViewController alloc]init];  secondVc.view.backgroundColor = [UIColor blueColor];  [self addChildViewController:secondVc];  MYThirdViewController *thirdVc = [[MYThirdViewController alloc]init];  thirdVc.view.backgroundColor = [UIColor yellowColor];  [self addChildViewController:thirdVc];  //默認(rèn)展示第一個(gè)子控制器  [self scrollViewDidEndDecelerating:self.containerScrollerView];}

按鈕點(diǎn)擊事件實(shí)現(xiàn) , 代理方法實(shí)現(xiàn)

//選擇欄按鈕點(diǎn)擊事件- (void)buttonClick:(UIButton *)button{  [self.containerScrollerView setContentOffset:CGPointMake(button.tag *kScreenWidth, 0) animated:YES];}//滑動(dòng)減速時(shí)調(diào)用- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{  //獲取contentOffset  CGPoint currentOffset = scrollView.contentOffset;  NSInteger page = currentOffset.x / kScreenWidth;  //取出對(duì)應(yīng)控制器  UIViewController *viewController = self.childViewControllers;  //添加到scrollView容器  //  if (![viewController isViewLoaded]) {  [self.containerScrollerView addSubview:viewController.view];  viewController.view.frame = CGRectMake(page *kScreenWidth, 0,kScreenWidth, kScreenHeight);  //  }}

目錄

iOS,多控制器,滑動(dòng)動(dòng)畫

效果

iOS,多控制器,滑動(dòng)動(dòng)畫

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美成年性h版影视中文字幕 | 国产又白又嫩又紧又爽18p | 色婷婷tv | 禁漫天堂久久久久久久久久 | 成人在线影视 | 88xx成人永久免费观看 | 中文字幕亚洲视频 | 国产精品成人亚洲一区二区 | 精品一区二区三区在线视频 | 国产999在线 | 亚洲第一成网站 | 一级爱片 | 黄色av片三级三级三级免费看 | 在线 日本 制服 中文 欧美 | 美女黄影院 | www.91sp| 免费观看高清视频网站 | 久久精品视频网站 | 国产精品99久久99久久久二 | 成人毛片在线免费观看 | 国产精品免费麻豆入口 | 热久久成人 | 欧美精品一级片 | 88xx成人永久免费观看 | 国产免费视频在线 | 偷偷草网站 | av在线等 | 亚洲视频在线观看免费视频 | avav在线播放 | 欧美女孩videos | 精品乱码久久久久 | 黄网站免费观看视频 | 国产精品99久久久久久久vr | 精品国产乱码久久久久久丨区2区 | 污污短视频 | 国产精品免费大片 | 久久久久久久久久亚洲 | 国产精品一区二区三区99 | 免费观看一级淫片 | 色视频在线 | 国产精品欧美日韩一区二区 |