首先呢 這裏說的是加載網絡的pdf 可不是本地的哦 網絡
新建一個view 主要是用於循環渲染繪製pdf單頁面內容ui
.h重寫init方法 用於自定義 接收docRef文件和傳入的page頁面atom
- (instancetype)initWithFrame:(CGRect)frame documentRef:(CGPDFDocumentRef)docRef andPageNum:(int)page;url
.m頁面呢 實現方法 而且須要屬性方法spa
CGPDFDocumentRef documentRef; 這個是pdf接收的doc文件類型日誌
NSInteger pageNum; 接收當前的pagecode
- (instancetype)initWithFrame:(CGRect)frame documentRef:(CGPDFDocumentRef)docRef andPageNum:(int)page{orm
self = [super initWithFrame:frame];server
if(self){文檔
documentRef= docRef;
_pageNum= page;
self.backgroundColor= [UIColor whiteColor];
}
return self;
}
而後呢 由於是循環渲染繪製 須要重寫 drawRect 重寫此方法,執行重繪任務
drawRect調用是在Controller->loadView,,Controller->viewDidLoad 兩方法以後調用的
- (void)drawRect:(CGRect)rect {
[self drawPDFIncontext:UIGraphicsGetCurrentContext()];//將當前的上下文環境傳遞 新建方法
}
好了 最重要的一部分要來了
- (void)drawPDFIncontext:(CGContextRef)context {
//調整圖形的位置
//Quartz座標系和UIView座標系不同所致,調整座標系,使pdf正立
CGContextTranslateCTM(context,0.0,self.frame.size.height);
//圖形呈正立顯示
CGContextScaleCTM(context,1.0, -1.0);
/獲取指定頁的pdf文檔
CGPDFPageRef pageRef =CGPDFDocumentGetPage(documentRef,_pageNum);
//記錄當前繪製環境,防止屢次繪畫
CGContextSaveGState(context);
//建立一個仿射變換,該變換基於將PDF頁的BOX映射到指定的矩形中。
CGAffineTransform pdfTransForm =CGPDFPageGetDrawingTransform(pageRef,kCGPDFCropBox,self.bounds,0,true);
CGContextConcatCTM(context, pdfTransForm);
//將pdf繪製到上下文中
CGContextDrawPDFPage(context, pageRef);
//將pdf保存
CGContextRestoreGState(context);
}
好了 自定義的view 實現的方法就是這些了 而後回到vc頁面去
主要是在.m裏面的 viewDidLoad
下面 我要開始了哦
翻頁咱們能夠想到不少 好比UIPageViewController UICollectionView等 這裏我主要說的是UIPageViewController
@property (nonatomic,strong) UIPageViewController *pageVC;
@property (nonatomic,strong) NSMutableArray *pdfArr;
這個大家都懂吧
self.pdfArr = [NSMutableArray array];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey];
self.pageVC = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
_pageVC.view.frame = self.view.bounds;
_pageVC.delegate = self;
_pageVC.dataSource = self;
[self addChildViewController:_pageVC];
CGPDFDocumentRef pdfRef = [self pdfRefByDataByUrl:@"http://eb18036.ebenny.com/new_huasun_server/upload//pdf//1527047215118.pdf"];
size_t count = CGPDFDocumentGetNumberOfPages(pdfRef);//這個位置主要是獲取pdf頁碼數;
for (int i = 0; i < count; i++) {
UIViewController *pdfVC = [[UIViewController alloc] init];
LLPDFView *pdfView = [[LLPDFView alloc]initWithFrame:self.view.bounds documentRef:pdfRef andPageNum:i+1];
[pdfVC.view addSubview:pdfView];
[_pdfArr addObject:pdfVC];
}
[_pageVC setViewControllers:[NSArray arrayWithObject:_pdfArr[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[self.view addSubview:_pageVC.view];
- (CGPDFDocumentRef)pdfRefByDataByUrl:(NSString *)aFileUrl {
NSURL*url = [NSURL URLWithString:aFileUrl];
CFURLRef refURL = (__bridge_retained CFURLRef)url;
CGPDFDocumentRef document =CGPDFDocumentCreateWithURL(refURL);
if(refURL){
CFRelease(refURL);
}
if(document) {
return document;//返回獲取到的數據
}else{
return NULL; //若是沒獲取到數據,則返回NULL,固然,你能夠在這裏添加一些打印日誌,方便你發現問題
}
}
- (UIViewController *)viewControllerAtIndex:(NSInteger)index
{
//Create a new view controller and pass suitable data.
if (([_pdfArr count] == 0 )|| (index > [_pdfArr count]) ) {
return nil;
}
NSLog(@"index = %ld",(long)index);
return (UIViewController *)_pdfArr[index];
}
- (NSUInteger) indexOfViewController:(UIViewController *)viewController
{
return [self.pdfArr indexOfObject:viewController];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(UIViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [_pdfArr count]){
return nil;
}
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(UIViewController *)viewController];
if ((index == 0 ) || (index == NSNotFound)){
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}