UIPageViewController的简单使用
2015-11-27 22:54
513 查看
UIPageViewController为我们提供一种类似翻书的效果。
在模态推出VC的时候我们应该都设置过modalTransitionStyle属性吧?其中就有翻书的效果。而UIPageViewController所实现的主要效果就是这种翻书效果。
不废话了,直接上代码:
ViewController.m文件:
MoreViewController.h文件:
MoreViewController.m文件:
大致就是这两种效果。
在模态推出VC的时候我们应该都设置过modalTransitionStyle属性吧?其中就有翻书的效果。而UIPageViewController所实现的主要效果就是这种翻书效果。
不废话了,直接上代码:
ViewController.m文件:
#import "ViewController.h" #import "MoreViewController.h" @interface ViewController ()<UIPageViewControllerDataSource> @property (nonatomic, strong) UIPageViewController *pageVC; @property (nonatomic, strong) NSMutableArray *muArr;//内容数组 @end @implementation ViewController - (void)handleData { NSMutableArray *tempArr = [NSMutableArray array]; for (int i = 0; i < 10; i++) { NSString *str = [[NSString alloc] initWithFormat:@"这是第%d页",i]; [tempArr addObject:str]; } self.muArr = [NSMutableArray arrayWithArray:tempArr]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; [self setAutomaticallyAdjustsScrollViewInsets:NO]; [self handleData]; // 设置UIPageViewController的各选项 NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey]; // 实例化UIPageViewController对象 //StyleScroll是有小点而无翻页效果;pageCurl是有翻页效果而无指示器(一排小点,<span style="font-family: Arial, Helvetica, sans-serif;">类似UIPageControl</span><span style="font-family: Arial, Helvetica, sans-serif;">);</span> self.pageVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options]; // 设置数据源代理 self.pageVC.dataSource = self; // 定义pageVC的frame //[self.pageVC.view setFrame:CGRectMake(0, 20, 375, 300)]; self.pageVC.view.frame = CGRectMake(0,0, self.view.frame.size.width, 300); // UIPageViewController对象要显示的页数据为一个NSArray。 // 定义UIPageViewController对象显示样式为一页(options参数设置)。 // 如果要显示2页,NSArray中需要2个相应页数据。 MoreViewController *moreVC = [self viewControllerAtIndex:0];//第一页 NSArray *viewArr = [NSArray arrayWithObject:moreVC]; [self.pageVC setViewControllers:viewArr direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {//block内容可置nil }]; [self addChildViewController:self.pageVC]; [self.view addSubview:self.pageVC.view]; } - (MoreViewController *)viewControllerAtIndex:(NSUInteger)index {//自己写的方法,返回VC对象 if (self.muArr.count == 0 || index >= self.muArr.count) { return nil; } // MoreViewController *moreVC = [[MoreViewController alloc] init]; moreVC.labelStr = [self.muArr objectAtIndex:index]; moreVC.pageIndex = index; return moreVC; } - (NSUInteger)indexOfViewController:(MoreViewController *)viewController {//自己写的方法 return [self.muArr indexOfObject:viewController.labelStr]; } // 返回上一个ViewController对象,代理方法 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { NSUInteger index = [self indexOfViewController:(MoreViewController *)viewController]; if (index == 0 || index == NSNotFound) { return nil; } index--; // 返回的ViewController,将被添加到相应的UIPageViewController对象上。 // UIPageViewController对象会根据UIPageViewControllerDataSource协议方法,自动来维护次序。 // 不用我们去操心每个ViewController的顺序问题。 return [self viewControllerAtIndex:index]; } // 返回下一个ViewController对象,代理方法 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { NSUInteger index = ((MoreViewController *)viewController).pageIndex; if (index == NSNotFound) { return nil; } index += 1; if (index == self.muArr.count) { return nil; } return [self viewControllerAtIndex:index]; } //当pageVC的translationstyle设为UIPageViewControllerTransitionStylePageCurl 时,页面下方不会出现小点(类似UIPageControl); //即如果想有翻页效果,以下的方法没用,系统不会使页面上移出现小点; - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController { return self.muArr.count; } - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController { return 0;//全体小点循环右移的位数; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
MoreViewController.h文件:
#import <UIKit/UIKit.h> @interface MoreViewController : UIViewController @property (nonatomic, strong) UILabel *myLabel; @property (nonatomic, copy) NSString *labelStr; @property (nonatomic, assign) NSUInteger pageIndex; @end
MoreViewController.m文件:
#import "MoreViewController.h" @interface MoreViewController () @end @implementation MoreViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //self.view.backgroundColor = [UIColor whiteColor]; self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 30)]; self.myLabel.backgroundColor = [UIColor orangeColor]; self.myLabel.text = self.labelStr; [self.view addSubview:self.myLabel]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
大致就是这两种效果。
相关文章推荐
- iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建)
- iOS开发UI篇—UITabBarController简单介绍
- 《apue》读书笔记 第三章 文件I/O
- UI 之CGAffineTransform介绍
- Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Not unique table/alias: 'e'
- UI--手势方法介绍
- [leetcode] 96. Unique Binary Search Trees
- cf div2 #333 D. Lipshitz Sequence (单调栈)
- [POJ]2299 Ultra-QuickSort
- 利用NGUI插件,制作滚动列表
- 关于UGUI和NGUI监听的研究
- 151127UITableView顶部有一个空白区域
- UITableViewCell状态切换效果
- UICollectionViewFlowLayout流布局制作图片瀑布流
- 自定义UITableViewCell类 && 自适应高度
- UITabBarController图标改颜色
- iOS UI设计: 仿QQ左边抽屉侧栏侧拉效果
- iOS 开发 UI 搭建心得(一)—— 驾驭 StoryBoard
- codeforces 601B Lipshitz Sequence
- UIControlEventEditingDidEnd和UIControlEventEditingDidOnExit各自的触发时机