iOS實(shí)現(xiàn)多控制器切換效果
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)多控制器切換效果的具體代碼,供大家參考,具體內(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[ page ];
? ? //添加到scrollView容器
? ? // ? ?if (![viewController isViewLoaded]) {
? ? [self.containerScrollerView addSubview:viewController.view];
? ? viewController.view.frame = CGRectMake(page *kScreenWidth, 0,kScreenWidth, kScreenHeight);
? ? // ? ?}
}目錄

效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 實(shí)例講解iOS中的UIPageViewController翻頁(yè)視圖控制器
- iOS開發(fā)中導(dǎo)航控制器的基本使用教程
- 詳解iOS開發(fā)中使用storyboard創(chuàng)建導(dǎo)航控制器的方法
- iOS應(yīng)用開發(fā)中UITabBarController標(biāo)簽欄控制器使用進(jìn)階
- 學(xué)習(xí)iOS自定義導(dǎo)航控制器UINavigationController
- iOS開發(fā)中的ViewController轉(zhuǎn)場(chǎng)切換效果實(shí)現(xiàn)簡(jiǎn)介
- iOS AVPlayer切換播放源實(shí)現(xiàn)連續(xù)播放和全屏切換的方法
- iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
- 比較IOS開發(fā)中常用視圖的四種切換方式
- iOS 頁(yè)面滑動(dòng)與標(biāo)題切換顏色漸變的聯(lián)動(dòng)效果實(shí)例
相關(guān)文章
iOS 簡(jiǎn)約日歷控件EBCalendarView的實(shí)現(xiàn)代碼
本篇文章主要介紹了iOS 簡(jiǎn)約日歷控件EBCalendarView的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
iOS音樂(lè)播放器實(shí)現(xiàn)代碼完整版
這篇文章主要為大家詳細(xì)介紹了iOS音樂(lè)播放器實(shí)現(xiàn)代碼完整版,包括音頻列表、播放器、后臺(tái)播放、鎖屏播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
iOS實(shí)現(xiàn)攝像頭實(shí)時(shí)采集圖像
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)攝像頭實(shí)時(shí)采集圖像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
iOS 9 更新之Safari廣告攔截器(Content Blocker)開發(fā)教程
這篇文章主要介紹了iOS 9 更新之Safari廣告攔截器(Content Blocker)開發(fā)教程的相關(guān)資料,需要的朋友可以參考下2015-08-08
iOS實(shí)現(xiàn)UITableView左滑刪除復(fù)制即用功能
這篇文章主要介紹了iOS實(shí)現(xiàn)UITableView左滑刪除復(fù)制即用功能,在項(xiàng)目開發(fā)中經(jīng)常會(huì)用到這樣的需求,下面小編把實(shí)現(xiàn)代碼分享給大家,需要的朋友可以參考下2017-09-09

