iOS UIPageViewController模仿真实翻页效果
来源:互联网 发布:淘宝上解id锁是真的吗 编辑:程序博客网 时间:2024/04/30 04:19
最近研究了一下UIPageViewController,这是系统提供的一个模仿真实翻页效果的控件,在分享出来希望对大家有帮助,如果有不对的地方或者更好的方法希望大家多多指教,下面让我们简单的了解一下UIPageViewController的使用
@interface UIPageViewController : UIViewController
UIPageViewController继承于UIViewController,下面是它的一些属性和常用方法
- 方法
书脊位置枚举,决定首页显示的视图个数
typedef NS_ENUM(NSInteger, UIPageViewControllerSpineLocation) {
UIPageViewControllerSpineLocationNone = 0, //没有书脊,滚动显示视图
UIPageViewControllerSpineLocationMin = 1, // 书脊居左或上,首页显示一个视图
UIPageViewControllerSpineLocationMid = 2, // 书脊居中,首页显示两个视图
UIPageViewControllerSpineLocationMax = 3 // 书脊居右或下,显示一个视图
};
翻页方式枚举
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationDirection) {
UIPageViewControllerNavigationDirectionForward, //从左往右(或从下往上)翻页
UIPageViewControllerNavigationDirectionReverse //从右往左(或从上往下)翻页
};
翻转样式枚举
typedef NS_ENUM(NSInteger, UIPageViewControllerTransitionStyle) {
UIPageViewControllerTransitionStylePageCurl = 0, //翻书效果样式
UIPageViewControllerTransitionStyleScroll = 1 //滑屏效果样式
};
导航方向枚举
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationOrientation) {UIPageViewControllerNavigationOrientationHorizontal = 0, //水平
UIPageViewControllerNavigationOrientationVertical = 1 //垂直
};
初始化方法
- (instancetype)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(nullable NSDictionary
下面是代码
1、首先先创承载数据的Controller
#import <UIKit/UIKit.h>@interface ImageViewController : UIViewController@property(nonatomic ,strong)UIImageView *imageView;@property(nonatomic ,strong)UIImage *image;@end#import "ImageViewController.h"@interface ImageViewController ()@end@implementation ImageViewController-(id)init{ self = [super init]; if (self) { } return self;}-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; _imageView = [[UIImageView alloc]initWithFrame:self.view.bounds]; _imageView.image = _image; _imageView.center = self.view.center; _imageView.userInteractionEnabled = YES; [self.view addSubview:_imageView];}@end
创建承载UIPageViewController的Controller
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIPageViewControllerDelegate,UIPageViewControllerDataSource> @property(nonatomic ,strong) UIPageViewController *pageViewController; @property(nonatomic ,strong) NSMutableArray *dataArray; @end
@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //创建数据源 _dataArray = [NSMutableArray array]; for (int i = 0; i < 13; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",i]]; ImageViewController *imageVC = [[ImageViewController alloc]init]; imageVC.image = image; [_dataArray addObject:imageVC]; } //设置第三个参数 NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey: UIPageViewControllerOptionSpineLocationKey]; //初始化UIPageViewController _pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options]; //指定代理 _pageViewController.delegate = self; _pageViewController.dataSource = self; //设置frame _pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); //是否双面显示,默认为NO _pageViewController.doubleSided = NO; //设置首页显示数据 ImageViewController *imageViewController = [self createImage:0]; NSArray *array = [NSArray arrayWithObjects:imageViewController, nil]; [_pageViewController setViewControllers:array direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil]; //添加pageViewController到Controller [self addChildViewController:_pageViewController]; [self.view addSubview:_pageViewController.view];}//获取指定显示controller-(ImageViewController *)createImage:(NSInteger)integer{ return [_dataArray objectAtIndex:integer];}//获取显示controller元素下标-(NSInteger)integerWithController:(ImageViewController *)vc{ return [_dataArray indexOfObject:vc];}#pragma mark - UIPageViewControllerDataSource//显示前一页- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{ NSInteger integer = [self integerWithController:(ImageViewController *)viewController]; if (integer == 0 || integer == NSNotFound) { return nil; } integer--; return [self createImage:integer];}//显示下一页- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{ NSUInteger index = [self integerWithController:(ImageViewController *)viewController]; if (index == NSNotFound) { return nil; } index++; if (index == _dataArray.count) { return nil; } return [self createImage:index];}//返回页控制器中页的数量-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{ return 13;}//返回页控制器中当前页的索引-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{ return 0;}#pragma mark - UIPageViewControllerDelegate//翻页视图控制器将要翻页时执行的方法- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers{ NSLog(@"将要翻页也就是手势触发时调用方法");}//可以通过返回值重设书轴类型枚举- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation{ return UIPageViewControllerSpineLocationMin;}//返回页控制器中控制器的页内容控制器数-(UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController{ return _dataArray.count;}//防止上一个动画还没有结束,下一个动画就开始了//当用户从一个页面转向下一个或者前一个页面,或者当用户开始从一个页面转向另一个页面的途中后悔 了,并撤销返回到了之前的页面时,将会调用这个方法。假如成功跳转到另一个页面时,transitionCompleted 会被置成 YES,假如在跳转途中取消了跳转这个动作将会被置成 NO。-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{ if(finished && completed) { // 无论有无翻页,只要动画结束就恢复交互。 pageViewController.view.userInteractionEnabled = YES; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
- iOS UIPageViewController模仿真实翻页效果
- UIPageViewController 实现从中间翻页效果
- UIPageViewController实现书脊在中间的翻页效果
- ios教程lwxshow.com-PDF UIPageViewController实现翻页浏览
- ios中关于UIPageViewController边缘自动翻页去除的问题
- ios中关于UIPageViewController边缘自动翻页去除的问题
- UIPageViewController翻页背景改变
- UIPageViewController 禁用翻页手势
- iOS 书翻页效果
- ios图书翻页效果
- ios 翻页效果
- iOS 书翻页效果
- iOS UIPageViewController
- iOS翻页(PageCurl)效果总结
- iOS翻页(PageCurl)效果总结
- iOS翻页(PageCurl)效果总结
- ios的翻页动画效果
- ios 视图切换翻页效果
- vagrant 安装与配置(phpstorm)
- Linux ubuntu下svn的命令使用指南
- 在Linux上配置PySpark
- VC6.0如何插入条件断点
- Android中SharedPreferences用法
- iOS UIPageViewController模仿真实翻页效果
- 笔试面试——fork()
- Spring 事务管理 DataSourceTransactionManager 和 DataSourceTransactionManager
- HDU-1501 (POJ-2192) Zipper (DFS||DP)
- 下拉列表框Spinner
- [疯狂Java]JDBC:用blob处理多媒体类型数据
- Codeforces 345 div1
- IosApp开发简单实例
- Code Forces 645B Mischievous Mess Makers