iOS UIPageViewController

//
//  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

时间: 2024-08-07 21:34:34

iOS UIPageViewController的相关文章

iOS:UIPageViewController翻页控制器控件详细介绍

翻页控制器控件:UIPageViewController 介绍: 1.它是为我们提供了一种类似翻书效果的一种控件.我们可以通过使用UIPageViewController控件,来完成类似图书一样的翻页控制方式.使用Page View,用户可以方便的通过手势在多个页面之间导. 2.先假象一下,一本书大概可以分为:1.每一页.2.每一页中相应的数据. 使用UIPageViewController控件,也是类似的两个构成部分.要有一个书的框架,来控制页:每一页的内容.创建一个ViewControlle

Learn how to Use UIPageViewController in iOS

下面学习内容来自国外的IOS学习网站:The AppGuruz: UIPageViewController in iOS  也许需要FQ哦 认真做一遍上面入门UIPageController的教程,然后仔细思考一下代码的逻辑,你就会得到以下总结: 1.UIPageController有一个子View是UIScrollView,可以通过以下代码获取到: 1 for (UIView* view in pageController.view.subviews){ 2 if([view isKindOf

iOS 5 :一个UIPageViewController程序示例

原文:http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_UIPageViewController_Application 在Xcode中新建项目时,可以选择“Page-based Application”项目模板.可以利用这个模板创建一种“基于页”的应用程序,在1年的每个月中显示不同的页.奇怪的是,这是Xcode提供的唯一一个基于实例的模板而不是应用程序基本框架.这对于一开始学习时很有用,但除非你真的需要一个用12页来显示1年

iOS使用UIPageViewController结合多个UITableView后点击状态栏无法让UITableView置顶问题

页面结构:1个UIPageViewController含多个其他ViewController,每个ViewController中又包含了一个UITableView 问题:无法通过点击状态栏,让当前UITableView内容置顶 原因:UIPageViewController帮助我们管理了多个ViewController,本质上在UIPageViewController的view中包含多多个ViewController中的UITableView.由于每个UITableView的scrollsToT

ios开发 &lt;AppName&gt;-Prefix.pch文件的用法详解

我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个文件,很长一段时间里笔者都没觉得它碍事.直到有一天笔者学习NSLog看网上的教程,大家是怎样在最终提交应用的时候,一次性将NSLog语句移除.网上大多转来转去的方法,都是说把如下的语句 #ifdef DEBUG#    define DLog(...) NSLog(__VA_ARGS__)#else

iOS开发资料整理

Please help me contribute to this list, for non-experience iOS developers or someone who need a come-in-handy library list to refer or to use in their projects. Fork, edit and send a PR are all things you can do! Table of Contents UI Component// UI组件

很好的iOS学习资料

https://github.com/vsouza/awesome-ios 汇集了很多好的资料 https://github.com/vsouza/awesome-ios Skip to content This repository Pull requests Issues Gist You don’t have any verified emails. We recommend verifying at least one email. Email verification helps ou

Differences Between Xcode Project Templates for iOS Apps

Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xcode, you get to choose between several project templates, from the aptly named “Empty Application” to specialized things like an “OpenGL Game”. I noti

iOS UIKit:viewController之层次结构

ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁.且应用程序至少有一个view controller.每个view controller对象都负责和管理一个view对象,称此对象为root view,其组织和管理以root view为根的视图层次结构. 图 11 可以将view controller分为两种类型: 1) Content:用于管理视图中的分散view控件,这些分散view控件是app中的主要实体. 2) Container:作为一种容器,