UIPageViewController 分屏效果笔记,参考IOS开发指南。
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIPageViewControllerDataSource, UIPageViewControllerDelegate> { int pageIndex; } @property(strong, nonatomic) UIPageViewController *pageViewController; @end
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.frame = CGRectMake(0.0f, 0.0f, 300.0f, 400.0f); // UIPageViewControllerTransitionStylePageCurl 翻书效果 // UIPageViewControllerTransitionStyleScroll 滑屏效果 // UIPageViewControllerNavigationOrientationHorizontal 水平方向 // UIPageViewControllerNavigationOrientationVertical 垂直方向 self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; self.pageViewController.dataSource = self; self.pageViewController.delegate = self; UIStoryboard *mainBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *page0 = [mainBoard instantiateViewControllerWithIdentifier:@"page1"]; NSArray *viewArray = @[page0]; [self.pageViewController setViewControllers:viewArray direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; [self.view addSubview:self.pageViewController.view]; pageIndex = 1; } -(UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { pageIndex++; if(pageIndex > 2){ pageIndex = 2; return nil; } UIStoryboard *mainBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; NSString *Id = [NSString stringWithFormat:@"page%i", pageIndex]; NSLog(@"%@", Id); UIViewController *page = [mainBoard instantiateViewControllerWithIdentifier:Id]; return page; } -(UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { pageIndex--; if(pageIndex < 0){ pageIndex = 0; return nil; } UIStoryboard *mainBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; NSString *Id = [NSString stringWithFormat:@"page%i", pageIndex]; NSLog(@"%@", Id); UIViewController *page = [mainBoard instantiateViewControllerWithIdentifier:Id]; return page; } @end
时间: 2024-10-12 12:26:29