Current location - Health Preservation Learning Network - Healthy weight loss - UIPageViewController of iOS explains the page turning video controller.
UIPageViewController of iOS explains the page turning video controller.
I. Introduction

UIPageViewController is one of the few animated view controllers in iOS. Through it, you can create a scrolling view similar to the combination of UIScrollView and UIPageControl, and you can also create a cool page-turning view similar to a book effect.

UIPageViewController was originally introduced in iOS 5 SDK, which enables developers to use this ViewController to create paginated views. In iOS 6, this class has been updated to support scrolling transition effects. Using page view, users can easily navigate between multiple pages through gestures. The UIPageViewController is not only used to guide pages, but also used in many games, such as Angry Birds, to display the pages of level selection, and there are also books-related applications, all of which use this class to display the pages of books.

UIPageViewController is a highly configurable class, and you can configure it as follows:

Page turning direction-horizontal or vertical page turning style-book page turning or sliding page turning spine position-only book page turning style is valid for page spacing-only sliding page turning style is valid for defining page spacing.

UIPageViewController is similar to a view container, in which each specific view is maintained and managed by its own ViewController, and UIPageViewController is only responsible for coordination and animation. The following figure can well show the use structure of UIPageViewControlelr:

In the above figure, UIPageViewControllerDataSource protocol provides data support for UIPageViewController, and the data provided by DataSource protocol is maintained by each ViewController. Callbacks in UIPageViewControllerDelegate can monitor page turning and screen rotation. UIPageViewController presents the View data obtained from DataSource to view for the display of the current view controller.

For demonstration, we will create a simple application together. Of course, we won't demonstrate all the configuration details of UIPageViewController, but we will demonstrate the use of sliding page turning style to create wizard pages. But don't worry, you have a basic understanding of UIPageViewController, and I believe you can also explore other features.

Let's start!

Second, create a UIPageViewController.

First, create a new class as the controller of each page view in the page turning view controller, and make it inherit UIViewController:

ModelViewController.h

# Import

@ interface ModelViewController:uiview controller

+(ModelViewController *)creatWithIndex:(int)index;

@property(nonatomic,strong)ui label * index label;

@end

ModelViewController.m

# import "ModelViewController.h"

@interface ModelViewController()

@end

@ Implement Model View Controller

+(ModelViewController *)creatwinthindex:(int)index {

ModelViewController * con =[[ModelViewController alloc]init];

con . index label =[[UILabel alloc]initWithFrame:CGRectMake( 1 10,200, 100,30)];

con . index label . text =[ns string string with format:@ " page % d ",index];

[con . view addSubview:con . index label];

Return to con

}

- (void)viewDidLoad {

[super viewdidload];

//Make any additional settings after loading the view.

self . view . background color =[ui color red color];

}

@end

Implement the following code in the ViewController.m file attached to the project template:

# import "ViewController.h"

# import "ModelViewController.h"

//Abide by the agreement

@interface ViewController()

{

//Page Turn View Controller Object

UIPageViewController * _ pageview control;

//data source array

NSMutableArray * _ dataArray

}

@end

@ Implement View Controller

- (void)viewDidLoad {

[super viewdidload];

//initialization

_ pageViewControl =[[UIPageViewController alloc] initwithfransitionstyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:uipageviewcontrollerorientionhorizontal options:@ { U ipageview controlleroptions spinelocationkey:@ 0,uipageviewcontrolleroptions interpagespacingkey:@ 10 }];

self . view . background color =[ui color green color];

//Set the size of the page turning view.

_ pageview control . view . bounds = self . view . bounds;

//Set data source and proxy

_ pageview control . data source = self;

_ pageview control . delegate = self;

//Create the initial interface

ModelViewController * model =[ModelViewController creatWithIndex: 1];

//Set the initial interface

[_ pageview control setview controllers:@[model]direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];

//Set whether to display on both sides.

_ pageview control . doublesided = NO;

_ data array =[[NSMutableArray alloc]init];

[_ data array add object:model];

[self . view add subview:_ pageview control . view];

}

//The page turning controller turns pages forward. The view controller returned by this data source method is the view controller that displays the view.

-(nullable uiviewcontroller *) pageviewcontroller: (uipageviewcontroller *) pageviewcontroller before viewcontroller: (uiviewcontroller *) viewcontroller {

int index =(int)[_ data array index of object:view controller];

if (index==0) {

Return to nil

} Otherwise {

return _ data array[index- 1];

}

}

//The page turning controller performs backward page turning. The view controller returned by this data source method is the view controller that displays the view.

-(nullable uiviewcontroller *) pageviewcontroller: (uipageviewcontroller *) pageviewcontroller viewcontroller afterviewcontroller: (uiviewcontroller *) viewcontroller {

int index =(int)[_ data array index of object:view controller];

if (index==9) {

Return to nil

} Otherwise {

if(_ data array . count- 1 & gt; =(index+ 1)) {

return _ data array[index+ 1];

} Otherwise {

ModelViewController * model =[ModelViewController creatWithIndex:index+2];

[_ data array add object:model];

Return mode;

}

}

}

//Proxy method triggered by screen rotation

-(UIPageViewControllerSpineLocation)pageview controller:(UIPageViewController *)pageview controller spinelocationforinterface orientation:(uiinterface orientation)orientation {

Returns UIPageView ControllerSpineLocation min;

}

//Set the paging number of the paging controller.

-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageview controller {

Returns10;

}

//Set the initial paging point

-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageview controller {

Returns 0;

}

@end

The simplest example of page turning view controller is created above, and the effect is as follows:

Thirdly, the analysis of method usage in UIPageViewController.

//Create Page Turning View Controller Object

-(instance type)initwithhtransitionstyle:(UIPageViewControllerTransitionStyle)style navigation orientation:(UIPageViewControllerNavigationOrientation) Navigation orientation options: (nsdictionary * which can be null) options;

The above method is used to create a view controller object, in which the UIPageViewControllerTransitionStyle parameter sets the style of the page turning controller, which is enumerated as follows:

typedef NS_ENUM(NSInteger,UIPageViewControllerTransitionStyle){

UipageviewControllerTransitionstylePagecurl = 0,//Similar to the effect of turning pages in books.

UIPageviewControllerTransitionStyleScroll =1/Similar to the sliding effect of ScrollView.

};

If it is set to UI Page View ControllerTransitionStyleCurl, the page turning effect is shown in the following figure:

The UIPageViewControllerNavigationOrientation attribute in the initialization method above sets the direction of page turning, which is enumerated as follows:

typedef NS_ENUM(NSInteger,UIPageViewControllerNavigationOrientation){

UipageviewControllerNavigationOrientationHorizontal = 0,//Turn pages horizontally.

UipageviewControllerNavigationOrientationVertical =1//Turn pages vertically.

};

The options parameter is used to set the configuration dictionary of the paging controller, and the configuration key values that can be set are as follows:

//This key needs to be set to the `ns number object corresponding to the UIPageViewControllerOptions SpinlocationKey enumeration value, and the book axis of the page turning controller will be set later.

ns string * const uipageviewcontrolleroptions spinelocationkey;

//This key needs to be set to NSNumber type to set the spacing of each page view for scrolling view style.

ns string * const UIPageViewControllerOptionInterPageSpacingKey;

The following are some common properties and methods of UIPageViewController:

//Set the data source

@property (nullable, non-atomic, weak) iddelegate.

//Set the proxy

@property (nullable, non-atomic, weak) iddataSource.

//Get page turning style

@property (nonatomic,readonly)UIPageViewControllerTransitionStyle transitionStyle;

//Get the page turning direction

@property (nonatomic,readonly)UIPageViewControllerNavigationOrientation navigation orientation;

//Get the scroll type

@property (nonatomic,readonly)UIPageViewControllerSpineLocation spine location;

//Set whether to display on both sides.

@property (nonatomic,getter = is doubleSided)BOOL doubleSided;

//Set the view controller to be displayed.

-(void)setviewcontrollers:(nullable nsarray *)view controllers direction:(uipageviewcontrollernavigationdirection)direction animated:(bool) animated completion:(void(^ _ _ nullable)(bool finished))completion;

Only the spineLocation attribute above is somewhat difficult to understand, and its enumeration is as follows:

typedef NS_ENUM(NSInteger,UIPageViewControllerSpineLocation){

//For the SCrollView type sliding effect, the following enumeration values will be returned without scrolling.

UIPageViewControllerSpineLocationNone = 0,

//Rotate the interface to the left or up to display only one view at a time.

UIPageViewControllerSpineLocationMin = 1,

//Flip the interface with the middle as the axis to display two views at the same time.

UIPageViewControllerSpineLocationMid = 2,

//Flip the interface with the lower edge or the right edge as the axis, and only one view will be displayed at the same time.

UIPageViewControllerSpineLocationMax = 3

};

Modify the above sample code as follows:

- (void)viewDidLoad {

[super viewdidload];

//Make any additional settings after loading the view, usually from nib.

_ pageViewControl =[[UIPageViewController alloc] initwithfransitionstyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:uipageviewcontrolleroptionspinelocationkey:@ { U ipageviewcon trolleroptions:@ 2,UIPageViewControllerOptionInterPageSpacingKey:@ 10 }];

self . view . background color =[ui color green color];

_ pageview control . view . bounds = self . view . bounds;

_ pageview control . data source = self;

_ pageview control . delegate = self;

ModelViewController * model =[ModelViewController creatWithIndex: 1];

ModelViewController * model 2 =[ModelViewController creatWithIndex:2];

[_ pageViewControl setview controllers:@[model,model 2]direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];

_ pageview control . doublesided = YES;

_ data array =[[NSMutableArray alloc]init];

[_ data array add object:model];

[self . view add subview:_ pageview control . view];

}

-(UIPageViewControllerSpineLocation)pageview controller:(UIPageViewController *)pageview controller spinelocationforinterface orientation:(uiinterface orientation)orientation {

Returns UIPageView ControllerSpineLocationMid;

}

The operation effect is shown in the following figure:

Fourthly, the method analysis in UIPageViewControllerDataSource.

//Page forward to display ViewController.

-(nullable uiviewcontroller *) pageviewcontroller: (uipageviewcontroller *) pageviewcontroller Beforeviewcontroller: (uiviewcontroller *) viewcontroller;

//Return to the page to display the ViewController.

-(nullable uiviewcontroller *) pageviewcontroller: (uipageviewcontroller *) pageviewcontroller viewcontroller afterviewcontroller: (uiviewcontroller *) viewcontroller;

//Set the paging number of the paging controller.

-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController NS _ AVAILABLE _ IOS(6 _ 0);

//Set the highlight of the current paging controller.

-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController NS _ AVAILABLE _ IOS(6 _ 0);

Fifth, the method analysis in UIPageViewControllerDelegate.

//The method to be executed when the page turning view controller is about to turn pages.

-(void)pageview controller:(UIPageViewController *)pageview controller willTransitionToViewControllers:(NSArray *)pendingViewControllers NS _ AVAILABLE _ IOS(6 _ 0);

//Call back method after page-turning animation is completed.

-(void)pageViewController:(UIPageViewController *)pageViewController did finish hanimating:(BOOL)finished previousViewControllers:(NSArray *) previousViewControllers transition completed:(BOOL)completed;

//For the method to be returned when the screen defense line changes, you can reset the book axis type enumeration by returning the value.

-(UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spinelocationforinterfaceoption:(uiinterfaceotination)orientation;