怎么在iOS中利用pageViewController实现多视图滑动切换

发布时间:2021-03-26 17:22:08 作者:Leah
来源:亿速云 阅读:485

本篇文章为大家展示了怎么在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.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实现多视图滑动切换,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 怎么在iOS中利用UICollectionView实现横向滑动
  2. 怎么在iOS中利用SwiftUI实现颜色渐变填充效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:怎么在IDEA中使用maven实现tomcat热部署

下一篇:怎么在PHP7.1中使用openssl替换mcrypt

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》