码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UIPageViewController

时间:2016-11-30 13:28:05      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:complete   bsp   uiview   tco   demo   ati   技术分享   previous   枚举   

//
//  ViewController.m
//  DemoTest

#import "ViewController.h"
#import "PageChildViewController.h"

@interface ViewController ()<UIPageViewControllerDelegate, UIPageViewControllerDataSource>

@property (nonatomic, strong) UIPageViewController * pageViewController;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self setUpPageViewController];
}

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


/** UIPageViewController */
- (void)setUpPageViewController {
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
}

//翻页效果
//UIPageViewControllerTransitionStylePageCurl
//滚动效果
//UIPageViewControllerTransitionStyleScroll

//效果方向
//UIPageViewControllerNavigationOrientationHorizontal
//UIPageViewControllerNavigationOrientationVertical

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

//options: 这个参数是可选的,传入的是对UIPageViewController的一些配置组成的字典,不过这个参数只能以UIPageViewControllerOptionSpineLocationKey和UIPageViewControllerOptionInterPageSpacingKey这两个key组成的字典.
//1) UIPageViewControllerOptionSpineLocationKey 这个key只有在style是翻书效果UIPageViewControllerTransitionStylePageCurl的时候才有作用, 它定义的是书脊的位置,值对应着UIPageViewControllerSpineLocation这个枚举项,不要定义错了哦.
//2) UIPageViewControllerOptionInterPageSpacingKey这个key只有在style是UIScrollView滚动效果UIPageViewControllerTransitionStyleScroll的时候才有作用, 它定义的是两个页面之间的间距(默认间距是0).
//UIPageViewControllerOptionInterPageSpacingKey - 两者之间间距
//UIPageViewControllerOptionSpineLocationKey - 书脊的位置
//除了初始化方法系统还提供了一个属性
//
//@property (nonatomic, getter=isDoubleSided) BOOL doubleSided; // Default is ‘NO‘.
//
//这个属性默认为NO,如果我们当前屏幕仅展示一个页面那么不用设置这个属性,如果设置了UIPageViewControllerSpineLocationMid这个选项,效果是翻开的书这样屏幕展示的就是两个页面,这个属性就必须设置为YES了.
//
//此外还有一个重要方法:
//
//- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers
//                 direction:(UIPageViewControllerNavigationDirection)direction
//                  animated:(BOOL)animated
//                completion:(void (^ __nullable)(BOOL finished))completion;
//
//这个方法是设置UIPageViewController初始显示的页面,如果doubleSided设为YES了,那么viewControllers这个参数至少包含两个页面.


- (UIPageViewController *)pageViewController {
    if (!_pageViewController) {
        NSDictionary *options = @{UIPageViewControllerOptionInterPageSpacingKey : @(20)};
//        NSDictionary *options = @{UIPageViewControllerOptionSpineLocationKey : @(UIPageViewControllerSpineLocationMin)};
        self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:(UIPageViewControllerTransitionStyleScroll) navigationOrientation:(UIPageViewControllerNavigationOrientationHorizontal) options:options];
        _pageViewController.delegate = self;
        _pageViewController.dataSource = self;

//appearance 预设一个类的设置,不包括tintColor;
//        UIPageControl *pageControl = [UIPageControl appearance];
//        pageControl.pageIndicatorTintColor = [UIColor greenColor];
//        pageControl.currentPageIndicatorTintColor = [UIColor redColor];
//        pageControl.backgroundColor = [UIColor whiteColor];
        [_pageViewController setViewControllers:@[[self viewControllerAtIndex:0]] direction:(UIPageViewControllerNavigationDirectionForward) animated:YES completion:nil];
        _pageViewController.view.frame = self.view.bounds;
    }
    return _pageViewController;
}
#pragma mark - delegate
/** 开始滚动时 */
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
    NSInteger index = [(PageChildViewController *)pendingViewControllers.firstObject pageIndex];
    NSLog(@"-=-=-=-=-=000000000-=-=-==-=-==%ld", index);
}
/** 结束滚动时 */
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    NSInteger index = [(PageChildViewController *)previousViewControllers.firstObject pageIndex];
    NSLog(@"-=-=-=-=-=111111111-=-=-==-=-==%ld", index);
}
///** 横竖屏变化 */
//- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation {
//    
//}
///** 设置UIPageViewController支持的屏幕旋转类型 */
//- (UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController {
//    
//}
///** 设置应用程序当前的屏幕的方向 */
//- (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController {
//    
//}
#pragma mark - dataSource
/** 返回前一个页面,nil时不滚动 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSInteger index = [(PageChildViewController *)viewController pageIndex];
    if (index == 0) {
        return nil;
    }else {
        index--;
        return [self viewControllerAtIndex:index];
    }
}
/** 返回后一个页面,nil时不滚动 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSInteger index = [(PageChildViewController *)viewController pageIndex];
    if (index == 3) {
        return nil;
    }else {
        index++;
        return [self viewControllerAtIndex:index];
    }
}

///** Item总数 */
//- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
//    return 4;
//}
///** 返回默认展示的页面index */
//- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
//    return 0;
//}

- (UIViewController *)viewControllerAtIndex:(NSInteger)index {
    PageChildViewController *viewController = [[PageChildViewController alloc] init];
    viewController.pageIndex = index;
    viewController.view.backgroundColor = [self LPCColorRandom];
    return viewController;
}

- (UIColor *)LPCColorRandom {
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

@end
技术分享

iOS UIPageViewController

标签:complete   bsp   uiview   tco   demo   ati   技术分享   previous   枚举   

原文地址:http://www.cnblogs.com/yujidewu/p/6116895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!