网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在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 (){

 //// 记录当前页 当前标题位置

 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 *)pendingViewControllers {
 UIViewController *nextVC = [pendingViewControllers firstObject];
 NSInteger index = [self.controllersArr indexOfObject:nextVC];
 ld_currentIndex = index;

}

/// 滑动结束后
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)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.pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.

}

TitleCollectionViewCell
@implementation TitleCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame{
 self = [super initWithFrame:frame];
 if (self) {
 [self createView];

 }
 return self;

}

- (void)createView{
 self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
 [self.contentView addSubview:self.titleLabel];
 self.titleLabel.font = [UIFont systemFontOfSize:14];

}
@end

上述内容就是怎么在iOS中利用pageViewController实现多视图滑动切换,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


文章题目:怎么在iOS中利用pageViewController实现多视图滑动切换
网站网址:http://bjjierui.cn/article/gcches.html

其他资讯