iOS UIPageViewController 使用

主要視圖控制器MyPageViewController

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyPageViewController : UIViewController

@end

NS_ASSUME_NONNULL_END

#import "MyPageViewController.h"
#import "MyViewController.h" /// 復(fù)用的controller
#import "MyCollectionTitleCell.h"http:/// 標(biāo)題欄使用的collectionviewcell

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

 //// 記錄當(dāng)前頁 當(dāng)前標(biāo)題位置
 NSInteger gl_currentIndex;

}

@property (nonatomic, strong) UIPageViewController *pageViewController;
@property (nonatomic, strong) NSMutableArray *controllersArr;/// 控制器數(shù)組
@property (nonatomic, strong) NSMutableArray *titleArray; /// 標(biāo)題數(shù)組
@property (nonatomic, strong) UICollectionView *titleCollectionView; /// 標(biāo)題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布局相同則循環(huán)創(chuàng)建MyViewController 添加進(jìn)數(shù)組,,如果controller 布局不同 那就創(chuàng)建多個(gè)不同controller依次添加數(shù)組
    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];
}

/// 創(chuàng)建標(biāo)題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];
}

//// 標(biāo)題collectionview的協(xié)議方法
- (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;
}

//// 點(diǎn)擊標(biāo)題左右切換視圖控制器------------再也不用看到好幾個(gè)中間頁面從屏幕快速閃過了------
- (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];
}

/// 創(chuàng)建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];
}

/// 將要滑動(dòng)切換的時(shí)候
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
    UIViewController *nextVC = [pendingViewControllers firstObject];
    NSInteger index = [self.controllersArr indexOfObject:nextVC];
    gl_currentIndex = index;
}

/// 滑動(dòng)結(jié)束后
- (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);
    }
}

/// 設(shè)置默認(rèn)顯示的是哪個(gè)頁面(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 控制器 可根據(jù)不同需求單獨(dú)定制


#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

滑動(dòng)標(biāo)題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效果可用于左右滑動(dòng)請(qǐng)求新數(shù)據(jù)

顯示效果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過簡信或評(píng)論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容