網站首頁 編程語言 正文
本文實例為大家分享了iOS實現多控制器切換效果的具體代碼,供大家參考,具體內容如下
主控制器 ,管理控制器 .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 , 用于滑動
@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; }
初始化頂部選擇欄
//三個子控制器 - (NSArray *)VcNames { ? ? if (!_VcNames) { ? ? ? ? _VcNames = @[@"控制器一",@"控制器二",@"控制器三"]; ? ? } ? ? return _VcNames; } //點擊選擇欄 - (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}; } //初始化滑動容器 - (void)initScrollViewContainer { ? ? [self.view addSubview:self.containerScrollerView]; ? ? self.containerScrollerView.frame = CGRectMake(0,44,kScreenWidth, kScreenHeight ); } //添加子控制器 - (void)addChildControllers { ? ? //為了方便直觀 , 在此處設置背景色 ?(實際開發中,不能在這里設置 , 原因是這里只要調用到了控制器的view屬性 , 該控制器將會執行viewDidLoad方法 , 相當于直接一開始就將三個控制器的所有UI和網絡請求全加載完了 , 負荷會相當重) ? ? 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]; ? ? //默認展示第一個子控制器 ? ? [self scrollViewDidEndDecelerating:self.containerScrollerView]; }
按鈕點擊事件實現 , 代理方法實現
//選擇欄按鈕點擊事件 - (void)buttonClick:(UIButton *)button { ? ? [self.containerScrollerView setContentOffset:CGPointMake(button.tag *kScreenWidth, 0) animated:YES]; } //滑動減速時調用 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { ? ? //獲取contentOffset ? ? CGPoint currentOffset = scrollView.contentOffset; ? ? NSInteger page = currentOffset.x / kScreenWidth; ? ? //取出對應控制器 ? ? UIViewController *viewController = self.childViewControllers[ page ]; ? ? //添加到scrollView容器 ? ? // ? ?if (![viewController isViewLoaded]) { ? ? [self.containerScrollerView addSubview:viewController.view]; ? ? viewController.view.frame = CGRectMake(page *kScreenWidth, 0,kScreenWidth, kScreenHeight); ? ? // ? ?} }
目錄
效果
原文鏈接:https://blog.csdn.net/coderMy/article/details/52901100
相關推薦
- 2022-10-30 Go中的錯誤和異常處理最佳實踐方法_Golang
- 2022-05-06 如何利用Python處理excel表格中的數據_python
- 2022-04-02 詳解python中的IO操作方法_python
- 2022-10-19 Go?熱加載之fresh詳解_Golang
- 2022-11-13 Git實現克隆歷史的某個版本_相關技巧
- 2022-03-27 Android實現井字游戲_Android
- 2022-08-24 C++中的Reactor原理與實現_C 語言
- 2022-08-15 接口狀態與策略路由表
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支