UIPageViewController的用法

在地铁上看到看小说的翻页效果,研究了一下,发现苹果自带翻页神器UIPageViewController

UIPageViewController一些常用的属性:

  • 首页展示的页面的个数
typedef NS_ENUM(NSInteger, UIPageViewControllerSpineLocation) {
    UIPageViewControllerSpineLocationNone = 0, // Returned if 'spineLocation' is queried when 'transitionStyle' is not 'UIPageViewControllerTransitionStylePageCurl'.
    UIPageViewControllerSpineLocationMin = 1,  // Requires one view controller.
    UIPageViewControllerSpineLocationMid = 2,  // Requires two view controllers.
    UIPageViewControllerSpineLocationMax = 3   // Requires one view controller.
};   // Only pertains to 'UIPageViewControllerTransitionStylePageCurl'.
  • 翻页方式
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationDirection) {

UIPageViewControllerNavigationDirectionForward, //从左往右(或从下往上)翻页

UIPageViewControllerNavigationDirectionReverse //从右往左(或从上往下)翻页

};
  • 翻转的样式
typedef NS_ENUM(NSInteger, UIPageViewControllerTransitionStyle) {

UIPageViewControllerTransitionStylePageCurl = 0, //翻书效果样式

UIPageViewControllerTransitionStyleScroll = 1 //类似Scrollview的滑动效果

};

  • 上下或者左右翻页的枚举
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationOrientation) {

UIPageViewControllerNavigationOrientationHorizontal = 0, //水平

UIPageViewControllerNavigationOrientationVertical = 1 //垂直

};

常用的用法

  • 遵循page协议
@interface ViewController : UIViewController
@end
  • 初始化pageController
    NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey: UIPageViewControllerOptionSpineLocationKey];
    
    _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
    
    _pageViewController.delegate = self;
    _pageViewController.dataSource = self;
    _pageViewController.doubleSided = NO;
    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    
    UIViewController *vc = [self createViewController:0];
//首页的效果
    [_pageViewController setViewControllers:@[vc]
                                  direction:UIPageViewControllerNavigationDirectionReverse
                                   animated:YES
                                 completion:nil];
    
    [self.datas addObject:vc];
    
    for (int i = 0; i < 10; i++) {
        
        [self.datas addObject:[self createViewController:i]];
  • 翻到下一页
  //下一页
- (nullable UIViewController *) pageViewController:(nonnull UIPageViewController *)pageViewController viewControllerAfterViewController:(nonnull UIViewController *)viewController {
    NSInteger index = [self.datas indexOfObject:viewController];
    index ++;
    if (index == self.datas.count ) {
        return nil;
    }
    
    return [self.datas objectAtIndex:index];
}
  • 上一页
-(nullable UIViewController *) pageViewController:(nonnull UIPageViewController *)pageViewController viewControllerBeforeViewController:(nonnull UIViewController *)viewController {
    NSInteger index = [self.datas indexOfObject:viewController];
    index --;
    if (index < 0) {
        return nil;
    }
    
    return [self.datas objectAtIndex:index];
}
  • 返回页控制器中页的数量
-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return self.datas.count;
}

//返回页控制器中当前页的索引
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return 0;
}
  • 屏幕旋转代理方法,旋转方式
- (UIPageViewControllerSpineLocation) pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation{
    return UIPageViewControllerSpineLocationMin;
}

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
    NSLog(@"将要翻页也就是手势触发时调用方法");
}

你可能感兴趣的:(UIPageViewController的用法)