UIPageViewController基本使用
@interface ViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource> @property(nonatomic,strong)UIPageViewController *pageVC; @property(nonatomic,strong)NSMutableArray *dataArr; @end @implementation ViewController -(NSMutableArray *)dataArr{ if(!_dataArr){ _dataArr = [NSMutableArray array]; [_dataArr addObject:[MyViewController new]]; [_dataArr addObject:[MyViewController new]]; [_dataArr addObject:[MyViewController new]]; } return _dataArr; } - (void)viewDidLoad { [super viewDidLoad]; self.pageVC.view.bounds = CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT); } -(UIPageViewController *)pageVC{ if(!_pageVC){ _pageVC = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; _pageVC.delegate = self; _pageVC.dataSource = self; [self addChildViewController:_pageVC]; [self.view addSubview:_pageVC.view]; [_pageVC didMoveToParentViewController:self]; [_pageVC setViewControllers:@[self.dataArr[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; } return _pageVC; } #pragma mark UIPageViewControllerDelegate - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{ } #pragma mark UIPageViewControllerDataSource - (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{ NSInteger currentPage = [self.dataArr indexOfObject:viewController]; if(currentPage <= 0){ return nil; }else{ currentPage --; return self.dataArr[currentPage]; } } - (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{ NSInteger currentPage = [self.dataArr indexOfObject:viewController]; if(currentPage >= self.dataArr.count - 1){ return nil; }else{ currentPage ++; return self.dataArr[currentPage]; } }