美文网首页
iOS UIPageViewController 使用

iOS UIPageViewController 使用

作者: GuiiLiin | 来源:发表于2020-06-24 10:41 被阅读0次

主要视图控制器MyPageViewController

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyPageViewController : UIViewController

@end

NS_ASSUME_NONNULL_END

#import "MyPageViewController.h"
#import "MyViewController.h" /// 复用的controller
#import "MyCollectionTitleCell.h"/// 标题栏使用的collectionviewcell

@interface MyPageViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource,UICollectionViewDelegate,UICollectionViewDataSource>{

 //// 记录当前页 当前标题位置
 NSInteger gl_currentIndex;

}

@property (nonatomic, strong) UIPageViewController *pageViewController;
@property (nonatomic, strong) NSMutableArray *controllersArr;/// 控制器数组
@property (nonatomic, strong) NSMutableArray *titleArray; /// 标题数组
@property (nonatomic, strong) UICollectionView *titleCollectionView; /// 标题collectionview
@end


@implementation MyPageViewController

- (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.titleStr = 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:[MyCollectionTitleCell 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 {
    MyCollectionTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"titleReuse" forIndexPath:indexPath];
    cell.title_Label.text = self.titleArray[indexPath.row];
    
    if (indexPath.row == gl_currentIndex) {
        cell.title_Label.textColor = [UIColor orangeColor];
    }else{
        cell.title_Label.textColor = [UIColor blackColor];
    }
    return cell;
}

//// 点击标题左右切换视图控制器------------再也不用看到好几个中间页面从屏幕快速闪过了------
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *vc = [self.controllersArr objectAtIndex:indexPath.row];
    if (indexPath.row > gl_currentIndex) {
        [self.pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
            
        }];
        
    } else {
        
        [self.pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL finished) {
            
        }];
    }
    gl_currentIndex = indexPath.row;
    NSIndexPath *path = [NSIndexPath indexPathForRow:gl_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];
    gl_currentIndex = index;
}

/// 滑动结束后
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (completed) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:gl_currentIndex inSection:0];
        [self.titleCollectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        [self.titleCollectionView reloadData];
        
        NSLog(@">>>>>>>>> %ld", (long)gl_currentIndex);
    }
}

/// 设置默认显示的是哪个页面(controller)
- (void)setTheFirstPage{
    UIViewController *vc = [self.controllersArr objectAtIndex:gl_currentIndex];
    [self.pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}

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

@end

Common 控制器 可根据不同需求单独定制


#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyViewController : UIViewController

@property (nonatomic, copy) NSString *titleStr;

@end

NS_ASSUME_NONNULL_END

#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    mainLabel.textAlignment = NSTextAlignmentCenter;
    mainLabel.font = [UIFont systemFontOfSize:30];
    mainLabel.textColor = [UIColor orangeColor];
    mainLabel.text = self.titleStr;
    [self.view addSubview:mainLabel];
}


@end

滑动标题cell

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyCollectionTitleCell : UICollectionViewCell

/** title_Label */
@property (nonatomic, strong) UILabel *title_Label;

@end

NS_ASSUME_NONNULL_END

#import "MyCollectionTitleCell.h"


@implementation MyCollectionTitleCell


- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        UILabel *title_Label = [[UILabel alloc] init];
        title_Label.font = [UIFont systemFontOfSize:12];
        title_Label.textAlignment = NSTextAlignmentCenter;
        title_Label.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        self.title_Label = title_Label;
        [self.contentView addSubview:title_Label];
        
    }
    
    return self;
}

@end

UIPageViewController效果可用于左右滑动请求新数据

显示效果

相关文章

网友评论

      本文标题:iOS UIPageViewController 使用

      本文链接:https://www.haomeiwen.com/subject/oxtrfktx.html