IOS development-tiled navigation-Page-based navigation and case implementation

Source: Internet
Author: User

After iOS 5 is implemented based on paging navigation, you can use the paging controller (UIPageViewController) to build applications similar to the e-book effect. This is called a paging-based application. A paging application has many related view controllers.

 

The PageViewController needs to be placed in a parent View Controller. A subview controller is also available under the page controller. Each subview controller corresponds to a page in the figure. The required classes and protocols for applications based on paging navigation: UIPageViewControllerDataSource protocol and UIPageViewControllerDelegate protocol and UIPageViewController class. UIPageViewController does not have a corresponding View class. In the UIPageViewControllerDelegate delegation protocol, the most important method is pageViewController: spineLocationForInterfaceOrientation:, which sets the Spine Location and initializes the homepage Based on the orientation of the screen rotation. UIPageViewController has two common attributes: doubleSided and spineLocation ). 1. Double-sided display: When the page is displayed, the even page is displayed on the back. Figure 6-13 The right figure shows the case where doubleSided is set to YES. In Figure 6-14, doubleSided is set to NO (single-sided display). When one-sided display is displayed on the page, the back of the page is displayed, the content on the back is the image of the current page, which is opposite to the current content. 2. The position of the spine. The spine position is also an important attribute, but its spineLocation attribute is read-only. To set it, you must use the pageViewController: spineLocationForInterfaceOrientation: Method in the UIPageViewControllerDelegate delegation protocol. The position of the spine is defined by the enumeration UIPageViewControllerSpineLocation. The member variables of this enumeration type are as follows.

Next we will use the page navigation to implement the city information application. Use the Single View Application template to create a project named PageNavigation. You can copy it from the PageControlNavigation project by enabling MainStoryboard. select three view controllers on the storyboard, press Command + C to copy them, and then open MainStoryboard in PageNavigation. storyboard, press Command + V to paste it. In this way, the uidesign is over, and the following work is done by the Code. Let's take a look at ViewController. h code: [cpp] # import <UIKit/UIKit. h> @ interface ViewController: UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate> {// int pageIndex of the current page;} @ property (strong, nonatomic) UIPageViewController * pageViewController; @ end # import <UIKit/UIKit. h> @ interface ViewController: UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate> {// Int pageIndex of the current page;} @ property (strong, nonatomic) UIPageViewController * pageViewController; @ end in the above Code, ViewController implements the UIPageViewControllerDataSource and UIPageViewControllerDelegate protocols. The member variable pageIndex stores the index of the current page, and the pageViewController attribute stores the UIPageViewController instance. Next let's take a look at the program code ViewController. the viewDidLoad method of m: [cpp]-(void) viewDidLoad {[super viewDidLoad]; self. view. frame = CGRectMake (0.0f, 0.0f, 3200000f, then 0000f); self. pageViewController = [[UIPageViewController alloc] initWithTransitionStyle: UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options: nil]; self. pageViewController. delegate = self; Self. pageViewController. dataSource = self; UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName: @ "MainStoryboard" bundle: nil]; UIViewController * page1ViewController = [mainStoryboard comment: @ "page1"]; // The first view, the most PageViewController homepage NSArray * viewControllers = @ [page1ViewController]; [self. pageViewController setViewControllers: viewControllers ction: U IPageViewControllerNavigationDirectionForward animated: YES completion: NULL]; [self addChildViewController: self. pageViewController]; [self. view addSubview: self. pageViewController. view]; pageIndex = 0;}-(void) viewDidLoad {[super viewDidLoad]; self. view. frame = CGRectMake (0.0f, 0.0f, 3200000f, then 0000f); self. pageViewController = [[UIPageViewController alloc] initWithTransitionStyle: UIPageViewControl LerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options: nil]; self. pageViewController. delegate = self; self. pageViewController. dataSource = self; UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName: @ "MainStoryboard" bundle: nil]; UIViewController * page1ViewController = [mainStoryboard comment: @ "page1"]; // The first view, the most PageViewController homepage NSArray * viewControllers = @ [page1ViewController]; [self. pageViewController setViewControllers: viewControllers ction: UIPageViewControllerNavigationDirectionForward animated: YES completion: NULL]; [self addChildViewController: self. pageViewController]; [self. view addSubview: self. pageViewController. view]; pageIndex = 0;} in the above Code, initWithTransitionStyle: navigationOri Entation: options: constructor is used to create a UIPageViewController instance. initWithTransitionStyle is used to set the page flip style. The UIPageViewControllerTransitionStyle Enumeration type defines the following two flip styles. UIPageViewControllerTransitionStylePageCurl: Book flip effect style. UIPageViewControllerTransitionStyleScroll: Sliding Screen Effect style. NavigationOrientation sets the page turning direction. The UIPageViewControllerNavigationDirection Enumeration type defines the following two page turning methods. UIPageViewControllerNavigationDirectionForward: from left to right (or from bottom to top); UIPageViewControllerNavigationDirectionReverse: from right to left (or from top to bottom ). Code NSArray * viewControllers = @ [page1ViewController] is equivalent to NSArray * viewControllers = [NSArray arrayWithObject: page1ViewController, nil]. In UIPageViewController, The setViewControllers: direction: animated: completion: method is used to set the view displayed on the home page. Several views are displayed on the home page, which is related to the spine type. For UIPageViewControllerSpineLocationMin or UIPageViewControllerSpineLocationMax, a view is displayed on the home page. The [self addChildViewController: self. pageViewController] statement adds PageViewController to the parent View Controller. Let's take a look at the code about the UIPageViewControllerDataSource protocol implementation method in ViewController. m: [cpp] view plaincopyprint? -(UIViewController *) pageViewController :( UIPageViewController *) pageViewController viewControllerBeforeViewController :( UIViewController *) viewController {pageIndex-; if (pageIndex <0) {pageIndex = 0; return nil ;} UIStoryboard * mainStoryboard = [UIStoryboard layout: @ "MainStoryboard" bundle: nil]; NSString * pageId = [NSString stringWithFormat: @ "page % I", pageIndex + 1]; UIViewController * p VController = [mainStoryboard layout: pageId]; return pvController;}-(UIViewController *) pageViewController :( UIPageViewController *) pageViewController viewControllerAfterViewController :( UIViewController *) viewController {pageIndex ++; if (pageIndex> 2) {pageIndex = 2; return nil;} UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName: @ "MainStoryboard" Bundle: nil]; NSString * pageId = [NSString stringWithFormat: @ "page % I", pageIndex + 1]; UIViewController * pvController = [mainStoryboard metadata: pageId]; return pvController ;} in ViewController. in m, the code for implementing the delegate protocol UIPageViewControllerDelegate is as follows:-(UIPageViewControllerSpineLocation) pageViewController :( UIPageViewController *) pageViewController spineLocationForInterfaceOrienta Tion :( UIInterfaceOrientation) orientation {self. pageViewController. doubleSided = NO; return UIPageViewControllerSpineLocationMin;} since the attributes of spineLocation are read-only, you can only set the spine position in this method, this method dynamically sets the position of the spine based on the orientation of the screen rotation. For the implementation code, refer to the following code:-(callback) pageViewController :( UIPageViewController *) pageViewController spineLocationForInterfaceOrientation :( UIInterfaceOrientation) orientation {UIStoryboard * MainStoryboard = [UIStoryboard layout: @ "MainStoryboard" bundle: nil]; UIViewController * page1ViewController = [mainStoryboard layout: @ "page1"]; UIViewController * page2ViewController = [mainStoryboard layout: @ "page2"]; if (orientation = UIInterfaceOrientationLandscapeLeft | orientation = UIInterfaceOrientationLandscape Right) {// retrieve the first view controller, the most PageViewController homepage NSArray * viewControllers = @ [page1ViewController, page2ViewController]; [self. pageViewController setViewControllers: viewControllers ction: UIPageViewControllerNavigationDirectionForward animated: YES completion: NULL]; self. pageViewController. doubleSided = NO; return UIPageViewControllerSpineLocationMid;} // retrieve the first view controller, the most PageViewController homepage NSArray * v IewControllers = @ [page1ViewController]; [self. pageViewController setViewControllers: viewControllers ction: UIPageViewControllerNavigationDirectionForward animated: YES completion: NULL]; self. pageViewController. doubleSided = NO; return UIPageViewControllerSpineLocationMin;}-(UIViewController *) pageViewController :( UIPageViewController *) pageViewController viewControllerBeforeViewController :( UI ViewController *) viewController {pageIndex-; if (pageIndex <0) {pageIndex = 0; return nil;} UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName: @ "MainStoryboard" bundle: nil]; NSString * pageId = [NSString stringWithFormat: @ "page % I", pageIndex + 1]; UIViewController * pvController = [mainStoryboard metadata: pageId]; return pvController;}-(UIViewController *) PageViewController :( UIPageViewController *) pageViewController viewControllerAfterViewController :( UIViewController *) viewController {pageIndex ++; if (pageIndex> 2) {pageIndex = 2; return nil ;} UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName: @ "MainStoryboard" bundle: nil]; NSString * pageId = [NSString stringWithFormat: @ "page % I", pageIndex + 1]; UIViewController * pvController = [mai NStoryboard instantiateViewControllerWithIdentifier: pageId]; return pvController;} In ViewController. in m, the code for implementing the delegate protocol UIPageViewControllerDelegate is as follows:-(callback) pageViewController :( UIPageViewController *) pageViewController spineLocationForInterfaceOrientation :( UIInterfaceOrientation) orientation {self. pageViewController. doubleSided = NO; return UIPageViewControllerSpineLoca TionMin;} since the attributes of spineLocation are read-only, you can only set the position of the spine in this method. This method can dynamically set the position of the spine based on the orientation of the screen rotation, for implementation code, see the following code:-(response) pageViewController :( UIPageViewController *) pageViewController preview :( UIInterfaceOrientation) orientation {UIStoryboard * mainStoryboard = [UIStoryboard usage: @ "MainStoryboard" bundle: nil]; UIViewController * page1ViewController = [MainStoryboard comment: @ "page1"]; UIViewController * page2ViewController = [mainStoryboard comment: @ "page2"]; if (orientation = Hangzhou | orientation = UIInterfaceOrientationLandscapeRight) {// retrieve the first view controller. The most PageViewController homepage is NSArray * viewControllers = @ [page1ViewController, page2ViewController]; [sel F. pageViewController setViewControllers: viewControllers ction: UIPageViewControllerNavigationDirectionForward animated: YES completion: NULL]; self. pageViewController. doubleSided = NO; return UIPageViewControllerSpineLocationMid;} // retrieve the first view controller, which is the most PageViewController homepage NSArray * viewControllers = @ [page1ViewController]; [self. pageViewController setViewControllers: viewControllers ction: UIPageView ControllerNavigationDirectionForward animated: YES completion: NULL]; self. pageViewController. doubleSided = NO; return UIPageViewControllerSpineLocationMin;} This is only a basic implementation and should be determined based on the specific application. When tiled navigation is used, UIPageViewController usually does not need to rotate the screen, and the position of the spine is not set in the middle. After the code is compiled, check the effect.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.