写了一个小Demo,显示本地pdf格式文件,支持翻页、跳页、缩放。
先看一下效果图:
iOS开发,显示pdf格式文件方法有很多:
最简单的应该是UIWebVIEw,可以加载本地或网络pdf文件,支持上下滑动浏览、缩放。@H_404_14@优化一点的是用系统的QLPrevIEwController加载,实现起来也比较方便,支持上下滑动浏览,左后滑动可多pdf文件切换,同时支持原生的分享打印,QLPrevIEwController支持的文档格式也比较多,如pdf、doc、docx、xls、xlsx、txt、ppt、mp4...@H_404_14@上面两种都没有足够的自定义空间,不在这里做过多的介绍了,另外一种也就是本篇用到的iOS核心图形库:Core Graphics,绘制pdf文档。@H_404_14@简单说一下逻辑,根据本地路径获取到CGpdfdocumentRef,在drawRect中绘制上下文,画出pdf文件。通过UIScrollVIEw实现缩放,添加UIGestureRecognizer实现单击、双击、左滑、右滑功能。基于CATransition实现翻页动画。
下面贴上核心代码:
承载pdf文件视图的控制器:HWpdfbrowseVC
#import <UIKit/UIKit.h>@interface HWpdfbrowseVC : UIVIEwController@property (nonatomic,copy) Nsstring *filePath;@property (nonatomic,copy) Nsstring *filename;@end/*** ---------------分割线--------------- ***/#import "HWpdfbrowseVC.h"#import "HWpdfbrowseVIEw.h"#import "HWpdfbrowsetoolbar.h"#import "HWpdfbrowseScrollVIEw.h"#define KPicMaxScale 3.0#define KMainW [UIScreen mainScreen].bounds.size.wIDth#define KMainH [UIScreen mainScreen].bounds.size.height@interface HWpdfbrowseVC ()<uiscrollviewdelegate,HWpdfbroesetoolbarDelegate>@property (nonatomic,weak) HWpdfbrowseScrollVIEw *scrollVIEw;@property (nonatomic,weak) HWpdfbrowseVIEw *browseVIEw;@property (nonatomic,weak) HWpdfbrowsetoolbar *toolbar;@property (nonatomic,assign) CGfloat minZoomScale;@property (nonatomic,assign) CGfloat lastScrContX;@end@implementation HWpdfbrowseVC- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; //初始化 self.vIEw.backgroundcolor = [UIcolor whitecolor]; self.navigationItem.Title = _filename; //创建控件 [self creatControl];}- (voID)vIEwWilldisappear:(BOol)animated{ [super vIEwWilldisappear:animated]; //防止隐藏导航时,左滑返回导航消失 CGRect temNavbarFrame = self.navigationController.navigationbar.frame; temNavbarFrame.origin.y = 20; self.navigationController.navigationbar.frame = temNavbarFrame;}- (voID)creatControl{ //导航右侧按钮 UIbutton *deleteBtn = [[UIbutton alloc] initWithFrame:CGRectMake(9,40,40)]; deleteBtn.TitleLabel.Font = [UIFont systemFontOfSize:16.f]; [deleteBtn setTitle:@"跳页" forState:UIControlStatenormal]; [deleteBtn setTitlecolor:[UIcolor bluecolor] forState:UIControlStatenormal]; [deleteBtn addTarget:self action:@selector(navBtnOnClick) forControlEvents:UIControlEventtouchUpInsIDe]; UIVIEw *rightVIEw = [[UIVIEw alloc] initWithFrame:CGRectMake(0,40)]; [rightVIEw addSubvIEw:deleteBtn]; self.navigationItem.rightbarbuttonItem = [[UIbarbuttonItem alloc] initWithCustomVIEw:rightVIEw]; //scrollVIEw HWpdfbrowseScrollVIEw *scrollVIEw = [[HWpdfbrowseScrollVIEw alloc] initWithFrame:[UIScreen mainScreen].bounds]; scrollVIEw.delegate = self; scrollVIEw.backgroundcolor = [UIcolor blackcolor]; scrollVIEw.maximumZoomScale = KPicMaxScale; scrollVIEw.showsverticalScrollindicator = NO; scrollVIEw.showsHorizontalScrollindicator = NO; [self.vIEw addSubvIEw:scrollVIEw]; _scrollVIEw = scrollVIEw; //pdf视图 HWpdfbrowseVIEw *browseVIEw = [[HWpdfbrowseVIEw alloc] initWithfilePath:_filePath]; [scrollVIEw addSubvIEw:browseVIEw]; _browseVIEw = browseVIEw; //绘制pdf视图后缩放至屏幕完全居中显示 CGRect frame = browseVIEw.frame; frame.size.wIDth = browseVIEw.frame.size.wIDth > KMainW ? KMainW : browseVIEw.frame.size.wIDth; frame.size.height = frame.size.wIDth * (browseVIEw.frame.size.height / browseVIEw.frame.size.wIDth); if (frame.size.height > KMainH) { frame.size.height = KMainH; frame.size.wIDth = KMainH * (browseVIEw.frame.size.wIDth / browseVIEw.frame.size.height); } //根据缩放调整 _minZoomScale = frame.size.wIDth / browseVIEw.frame.size.wIDth; scrollVIEw.allowScrollScale = _minZoomScale; scrollVIEw.minimumZoomScale = _minZoomScale; scrollVIEw.zoomScale = _minZoomScale; //底部工具栏 HWpdfbrowsetoolbar *toolbar = [[HWpdfbrowsetoolbar alloc] initWithFrame:CGRectMake(0,KMainH - 49,KMainW,49) currentPage:_browseVIEw.currentPage totalPage:_browseVIEw.totalPages]; toolbar.delegate = self; [self.vIEw addSubvIEw:toolbar]; _toolbar = toolbar; //单击 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)]; tap.numberOftouchesrequired = 1; tap.numberOfTapsrequired = 1; [scrollVIEw addGestureRecognizer:tap]; //双击 UITapGestureRecognizer *tapDouble = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleClick)]; tapDouble.numberOfTapsrequired = 2; [scrollVIEw addGestureRecognizer:tapDouble]; [tap requireGestureRecognizerToFail:tapDouble]; //右滑手势 UISwipeGestureRecognizer *rightSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage)]; rightSwip.direction = UISwipeGestureRecognizerDirectionleft; [scrollVIEw addGestureRecognizer:rightSwip]; //左滑手势 UISwipeGestureRecognizer *leftSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(forwardPage)]; leftSwip.direction = UISwipeGestureRecognizerDirectionRight; [scrollVIEw addGestureRecognizer:leftSwip];}- (voID)navBtnOnClick{ [_toolbar showWindow];}//单击屏幕显示隐藏菜单- (voID)click{ CGfloat navbarY = _toolbar.frame.origin.y == KMainH - 49 ? -64 : 20; CGfloat toolbarY = _toolbar.frame.origin.y == KMainH - 49 ? KMainH : KMainH - 49; [UIVIEw animateWithDuration:0.25 animations:^{ CGRect temNavbarFrame = self.navigationController.navigationbar.frame; temNavbarFrame.origin.y = navbarY; self.navigationController.navigationbar.frame = temNavbarFrame; CGRect temToolbarFrame = _toolbar.frame; temToolbarFrame.origin.y = toolbarY; _toolbar.frame = temToolbarFrame; }];}//双击屏幕放大缩小图片- (voID)doubleClick{ [UIVIEw animateWithDuration:0.25f animations:^{ _scrollVIEw.zoomScale = _scrollVIEw.zoomScale == _minZoomScale ? KPicMaxScale : _minZoomScale; }];}//左滑事件- (voID)nextPage{ [_browseVIEw nextPage]; _toolbar.currentPage = _browseVIEw.currentPage;}//右滑事件- (voID)forwardPage{ [_browseVIEw prePage]; _toolbar.currentPage = _browseVIEw.currentPage;}#pragma mark - UICousebrowsetoolbarDelegate- (voID)browsetoolbar:(HWpdfbrowsetoolbar *)browsetoolbar dIDClickFinishbuttonWithPage:(Nsstring *)page{ _browseVIEw.currentPage = [page integerValue]; [_browseVIEw reloadVIEw]; [_toolbar dismissKeyboard]; _scrollVIEw.zoomScale = _minZoomScale;}- (voID)browsetoolbar:(HWpdfbrowsetoolbar *)browsetoolbar dIDPagebuttonWithAction:(BOol)nextPage{ if (nextPage) { [self nextPage]; }else { [self forwardPage]; } _scrollVIEw.zoomScale = _minZoomScale;}#pragma mark - uiscrollviewdelegate- (voID)scrollVIEwDIDZoom:(UIScrollVIEw *)scrollVIEw{ CGfloat offsetX = (scrollVIEw.bounds.size.wIDth > scrollVIEw.contentSize.wIDth) ? (scrollVIEw.bounds.size.wIDth - scrollVIEw.contentSize.wIDth) * 0.5 : 0.0; CGfloat offsetY = (scrollVIEw.bounds.size.height > scrollVIEw.contentSize.height) ? (scrollVIEw.bounds.size.height - scrollVIEw.contentSize.height) * 0.5 : 0.0; _browseVIEw.center = CGPointMake(scrollVIEw.contentSize.wIDth * 0.5 + offsetX,scrollVIEw.contentSize.height * 0.5 + offsetY - 64);}- (UIVIEw *)vIEwForZoomingInScrollVIEw:(UIScrollVIEw *)scrollVIEw{ return _browseVIEw;}- (voID)scrollVIEwDIDScroll:(UIScrollVIEw *)scrollVIEw{ if (scrollVIEw.isZooming) return; //允许翻页的偏移量 CGfloat movepadding = 70.f; //仿苹果原生相册,图片放大后,滑动前在边界时在可以翻页,这里加了±10的偏移量 if (scrollVIEw.contentOffset.x < - movepadding && _lastScrContX < 10) { _scrollVIEw.zoomScale = _minZoomScale; [self forwardPage]; } if (scrollVIEw.contentSize.wIDth - scrollVIEw.contentOffset.x < KMainW - movepadding && scrollVIEw.contentSize.wIDth != 0 && fabsf(_lastScrContX + KMainW - scrollVIEw.contentSize.wIDth) < 10) { _scrollVIEw.zoomScale = _minZoomScale; [self nextPage]; }}//滑动自然停止时调用- (voID)scrollVIEwDIDEndDecelerating:(UIScrollVIEw *)scrollVIEw{ _lastScrContX = scrollVIEw.contentOffset.x;}//滑动手动停止时调用- (voID)scrollVIEwDIDEndDragging:(UIScrollVIEw *)scrollVIEw willDecelerate:(BOol)decelerate{ _lastScrContX = scrollVIEw.contentOffset.x;}- (voID)dIDReceiveMemoryWarning { [super dIDReceiveMemoryWarning]; // dispose of any resources that can be recreated.}@end
pdf文件视图:HWpdfbrowseVIEw
#import <UIKit/UIKit.h>@interface HWpdfbrowseVIEw : UIVIEw{ CGpdfdocumentRef pdfdocumentRef;}@property (nonatomic,assign) NSInteger currentPage;@property (nonatomic,assign) NSInteger totalPages;- (ID)initWithfilePath:(Nsstring *)filePath;- (voID)reloadVIEw;- (voID)prePage;- (voID)nextPage;@end/*** ---------------分割线--------------- ***/#import "HWpdfbrowseVIEw.h"@implementation HWpdfbrowseVIEw- (ID)initWithfilePath:(Nsstring *)filePath{ pdfdocumentRef = [self createpdfFromExistfile:filePath]; self = [super initWithFrame:CGpdfpageGetBoxRect(CGpdfdocumentGetPage(pdfdocumentRef,1),kCGpdfMediaBox)]; return self;}- (CGpdfdocumentRef)createpdfFromExistfile:(Nsstring *)afilePath{ CFStringRef path = CFStringCreateWithCString(NulL,[afilePath UTF8String],kcfStringEnCodingUTF8); CFURLRef urlRef = CFURLCreateWithfileSystemPath(NulL,path,kcfURLPOSIXPathStyle,NO); CFRelease(path); CGpdfdocumentRef document = CGpdfdocumentCreateWithURL(urlRef); CFRelease(urlRef); _totalPages = CGpdfdocumentGetNumberOfPages(document); _currentPage = 1; if (_totalPages == 0) return NulL; return document;}- (voID)reloadVIEw{ [self setNeedsdisplay];}- (voID)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); [[UIcolor whitecolor] set]; CGContextFillRect(context,rect); CGContextTranslateCTM(context,0.0,rect.size.height); CGContextScaleCTM(context,1.0,-1.0); CGpdfpageRef page = CGpdfdocumentGetPage(pdfdocumentRef,_currentPage); CGAffinetransform pdftransform = CGpdfpageGetDrawingtransform(page,kCGPdfcropBox,rect,true); CGContextConcatCTM(context,pdftransform); CGContextDrawpdfpage(context,page);}//上一页- (voID)prePage{ if(_currentPage < 2) { UIAlertVIEw *alert = [[UIAlertVIEw alloc]initWithTitle:@"提示" message:@"已经第一页了!" delegate:self cancelbuttonTitle:@"确定" otherbuttonTitles: nil ]; [alert show]; return; } --_currentPage; [self reloadVIEw]; [self TransitionWithType:@"pageUnCurl" WithSubtype:kCATransitionFromright ForVIEw:self];}//下一页- (voID)nextPage{ if(_currentPage >= _totalPages) { UIAlertVIEw *alert = [[UIAlertVIEw alloc]initWithTitle:@"提示" message:@"已经最后一页了!" delegate:self cancelbuttonTitle:@"确定" otherbuttonTitles: nil ]; [alert show]; return; } ++_currentPage; [self reloadVIEw]; [self TransitionWithType:@"pageCurl" WithSubtype:kCATransitionFromright ForVIEw:self];}//设置翻页动画效果- (voID)TransitionWithType:(Nsstring *)type WithSubtype:(Nsstring *)subtype ForVIEw:(UIVIEw *)vIEw{ CATransition *animation = [CATransition animation]; animation.duration = 0.7f; animation.type = type; if (subtype) animation.subtype = subtype; animation.timingFunction = UIVIEwAnimationoptionCurveEaseInOut; [vIEw.layer addAnimation:animation forKey:@"animation"];}@end
Demo 下载链接
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的iOS实现PDF文件浏览功能全部内容,希望文章能够帮你解决iOS实现PDF文件浏览功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)