版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在iOS中利用pageViewController实现多视图滑动切换
本篇文章为大家展示了怎么在iOS中利用pageViewController实现多视图滑动切换,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。思路1:使用嵌套,collectionview嵌套,每个item中添加内容思路2:使用scrollview在上面创建一个一个的controller实现左右滑动当停留在第一页的时候,点击标题栏第五页,那么平移的过程就是第一页到第五页,所有的页面从屏幕快速闪过,并且看到现在很多APP都是这样的。在此之前我是用的思路2,为了避免跨页面切换出现的中间几个页面闪过的过程,直接把平移动画关闭了。直到使用了uipageViewController,赶紧把项目中的给换掉了代码不多150行以内#import
"ViewController.h"///
当前controller
#import
"MyViewController.h"
///
复用的controller
适用于每个控制器布局相同的情况下,,布局不同就创建不同的controller添加进来
#import
"TitleCollectionViewCell.h"///
标题栏使用的collectionviewcell
@interface
ViewController
()<UIPageViewControllerDelegate,UIPageViewControllerDataSource,UICollectionViewDelegate,UICollectionViewDataSource>{
////
记录当前页
当前标题位置
NSInteger
ld_currentIndex;
}
@property
(nonatomic,
strong)
UIPageViewController
*pageViewController;
@property
(nonatomic,
strong)
NSMutableArray
*controllersArr;///
控制器数组
@property
(nonatomic,
strong)
NSMutableArray
*titleArray;
///
标题数组
@property
(nonatomic,
strong)
UICollectionView
*titleCollectionView;
///
标题collectionview
@end
@implementation
ViewController
-
(void)viewDidLoad
{
[super
viewDidLoad];
self.view.backgroundColor
=
[UIColor
whiteColor];
self.navigationController.navigationBar.translucent
=
NO;
self.controllersArr
=
[NSMutableArray
array];
self.titleArray
=
[NSMutableArray
array];
////
如果controller布局相同则循环创建MyViewController
添加进数组,,如果controller
布局不同
那就创建多个不同controller依次添加数组
for
(int
i
=
0;
i
<
10;
i++)
{
MyViewController
*con
=
[[MyViewController
alloc]init];
[self.controllersArr
addObject:con];
NSString
*str
=
[NSString
stringWithFormat:@"第
%d
页",
i+1];
con.titlestring
=
str;
[self.titleArray
addObject:str];
}
[self
createCollectionView];
[self
createPageViewController];
[self
setTheFirstPage];
}
///
创建标题collectionview
-
(void)createCollectionView{
UICollectionViewFlowLayout
*lay
=
[[UICollectionViewFlowLayout
alloc]
init];
lay.itemSize
=
CGSizeMake(60,
30);
lay.minimumLineSpacing
=
0;
lay.minimumInteritemSpacing
=
0;
lay.scrollDirection
=
UICollectionViewScrollDirectionHorizontal;
self.titleCollectionView
=
[[UICollectionView
alloc]
initWithFrame:CGRectMake(0,
34,
375,
30)
collectionViewLayout:lay];
self.titleCollectionView.showsHorizontalScrollIndicator
=
NO;
self.titleCollectionView.backgroundColor
=
[UIColor
whiteColor];
self.titleCollectionView.delegate
=
self;
self.titleCollectionView.dataSource
=
self;
[self.titleCollectionView
registerClass:[TitleCollectionViewCell
class]
forCellWithReuseIdentifier:@"titleReuse"];
[self.navigationController.view
addSubview:self.titleCollectionView];
}
////
标题collectionview的协议方法
-
(NSInteger)collectionView:(UICollectionView
*)collectionView
numberOfItemsInSection:(NSInteger)section{
return
self.titleArray.count;
}
-
(UICollectionViewCell
*)collectionView:(UICollectionView
*)collectionView
cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
TitleCollectionViewCell
*cell
=
[collectionView
dequeueReusableCellWithReuseIdentifier:@"titleReuse"
forIndexPath:indexPath];
cell.titleLabel.text
=
self.titleArray[indexPath.row];
if
(indexPath.row
==
ld_currentIndex)
{
cell.titleLabel.textColor
=
[UIColor
orangeColor];
}else{
cell.titleLabel.textColor
=
[UIColor
blackColor];
}
return
cell;
}
////
点击标题左右切换视图控制器再也不用看到好几个中间页面从屏幕快速闪过了
-
(void)collectionView:(UICollectionView
*)collectionView
didSelectItemAtIndexPath:(NSIndexPath
*)indexPath{
UIViewController
*vc
=
[self.controllersArr
objectAtIndex:indexPath.row];
if
(indexPath.row
>
ld_currentIndex)
{
[self.pageViewController
setViewControllers:@[vc]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:^(BOOL
finished)
{
}];
}
else
{
[self.pageViewController
setViewControllers:@[vc]
direction:UIPageViewControllerNavigationDirectionReverse
animated:YES
completion:^(BOOL
finished)
{
}];
}
ld_currentIndex
=
indexPath.row;
NSIndexPath
*path
=
[NSIndexPath
indexPathForRow:ld_currentIndex
inSection:0];
[self.titleCollectionView
scrollToItemAtIndexPath:path
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
[self.titleCollectionView
reloadData];
}
///
创建pageViewController
-
(void)createPageViewController
{
NSDictionary
*option
=
[NSDictionary
dictionaryWithObject:[NSNumber
numberWithInteger:0]
forKey:UIPageViewControllerOptionInterPageSpacingKey];
_pageViewController
=
[[UIPageViewController
alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:option];
_pageViewController.delegate
=
self;
_pageViewController.dataSource
=
self;
[self
addChildViewController:_pageViewController];
[self.view
addSubview:_pageViewController.view];
}
///
展示上一页
-
(nullable
UIViewController
*)pageViewController:(UIPageViewController
*)pageViewController
viewControllerBeforeViewController:(UIViewController
*)viewController
{
NSInteger
index
=
[self.controllersArr
indexOfObject:viewController];
if
(index
==
0
||
(index
==
NSNotFound))
{
return
nil;
}
index--;
return
[self.controllersArr
objectAtIndex:index];
}
///
展示下一页
-
(nullable
UIViewController
*)pageViewController:(UIPageViewController
*)pageViewController
viewControllerAfterViewController:(UIViewController
*)viewController
{
NSInteger
index
=
[self.controllersArr
indexOfObject:viewController];
if
(index
==
self.controllersArr.count
-
1
||
(index
==
NSNotFound))
{
return
nil;
}
index++;
return
[self.controllersArr
objectAtIndex:index];
}
///
将要滑动切换的时候
-
(void)pageViewController:(UIPageViewController
*)pageViewController
willTransitionToViewControllers:(NSArray<UIViewController
*>
*)pendingViewControllers
{
UIViewController
*nextVC
=
[pendingViewControllers
firstObject];
NSInteger
index
=
[self.controllersArr
indexOfObject:nextVC];
ld_currentIndex
=
index;
}
///
滑动结束后
-
(void)pageViewController:(UIPageViewController
*)pageViewController
didFinishAnimating:(BOOL)finished
previousViewControllers:(NSArray<UIViewController
*>
*)previousViewControllers
transitionCompleted:(BOOL)completed
{
if
(completed)
{
NSIndexPath
*path
=
[NSIndexPath
indexPathForRow:ld_currentIndex
inSection:0];
[self.titleCollectionView
scrollToItemAtIndexPath:path
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
[self.titleCollectionView
reloadData];
NSLog(@">>>>>>>>>
%ld",
(long)ld_currentIndex);
}
}
///
设置默认显示的是哪个页面(controller)
-
(void)setTheFirstPage{
UIViewController
*vc
=
[self.controllersArr
objectAtIndex:ld_currentIndex];
[self.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 全球及中国模拟信号调节器行业市场现状供需分析及市场深度研究发展前景及规划可行性分析研究报告(2024-2030)
- 全球及中国棒球和垒球鞋行业市场现状供需分析及市场深度研究发展前景及规划可行性分析研究报告(2024-2030)
- 全球及中国板桩行业市场现状供需分析及市场深度研究发展前景及规划可行性分析研究报告(2024-2030)
- 2024-2030年羊奶酸奶行业市场现状供需分析及市场深度研究发展前景及规划投资研究报告
- 2024-2030年电子表行业风险投资发展分析及运作模式与投资融资研究报告
- 2024-2030年国内婴幼儿护理书籍行业市场发展分析及竞争格局与投资机会研究报告
- 2024-2030年医护辅助设备行业发展分析及投资价值研究咨询报告
- 2024-2029年煤制天然气项目可行性研究报告
- 2023-2024学年陕西省咸阳市兴平市中考押题语文预测卷含解析
- 2024年微生物杀虫剂项目创业投资方案
- 2024年微信视频号行业现状及前景预测
- 基于单片机的智能垃圾桶控制系统设计与实现
- 供应商审核计划表
- 《自我定位第一章》课件
- 健康管理八大经典计划书
- 协会社团成立大会议程
- 2020业务架构模型指南
- 中医药基层指导科工作制度
- 常用EXCEL函数使用详解课件
- 烈士陵园可行性研究报告
- 矿长的聘用合同
评论
0/150
提交评论