iOS下PDF显示
来源:互联网 发布:手机复制软件下载 编辑:程序博客网 时间:2024/05/17 08:03
.h
#import <UIKit/UIKit.h>
@interface PdfView : UIView
{
CGPDFDocumentRef pdf;
// int totalPages;
// int currentPage;
}
@property(nonatomic)int totalPages;
@property(nonatomic)int currentPage;
-(void)drawInContext:(CGContextRef)context;
-(void)goUpPage;
-(void)goDownPage;
@end
.m
#import "PdfView.h"
@implementation PdfView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(void)reloadView{
[selfsetNeedsDisplay];
}
-(void)goUpPage{
if(_currentPage <2)
return;
--_currentPage;
[selfreloadView];
}
-(void)goDownPage{
if(_currentPage >=_totalPages)
return;
++_currentPage;
[selfreloadView];
}
-(void)drawInContext:(CGContextRef)context
{
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGPDFPageRef page =CGPDFDocumentGetPage(pdf,_currentPage);
CGContextSaveGState(context);
CGAffineTransform pdfTransform =CGPDFPageGetDrawingTransform(page, kCGPDFCropBox,self.bounds,0, true);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
}
- (void)drawRect:(CGRect)rect {
[selfdrawInContext:UIGraphicsGetCurrentContext()];
}
- (id)initWithFrame:(CGRect)frame{
if ((self = [superinitWithFrame:frame]))
{
// Initialization code
if(self !=nil)
{
CFURLRef pdfURL =CFBundleCopyResourceURL(CFBundleGetMainBundle(),CFSTR("RegisterDelegate.pdf"),NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
_totalPages =CGPDFDocumentGetNumberOfPages(pdf);
_currentPage=1;
CFRelease(pdfURL);
}
}
return self;
}
@end
#import "ViewController.h"
#import "PdfView.h"
@interface ViewController ()
@property (nonatomic,strong) UISwipeGestureRecognizer *upSwipeGestureRecognizer;
@property (nonatomic,strong) UISwipeGestureRecognizer *downSwipeGestureRecognizer;
@property (nonatomic,strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@property (nonatomic,strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
@end
@implementation ViewController
{
CGPDFPageRef myPageRef;
PdfView *pdfView;
UILabel *pdfPageLable;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect frame =CGRectMake(10,10,self.view.bounds.size.width-20,self.view.bounds.size.height-20);
pdfView = [[PdfViewalloc] initWithFrame:frame];
pdfView.backgroundColor=[UIColorwhiteColor];
[self.viewaddSubview:pdfView];
pdfPageLable = [[UILabelalloc]initWithFrame:CGRectMake(20,70, 80, 35)];
pdfPageLable.text = [NSStringstringWithFormat:@"%d of %d",pdfView.currentPage,pdfView.totalPages];
pdfPageLable.textAlignment =NSTextAlignmentCenter;
pdfPageLable.layer.masksToBounds =YES;
pdfPageLable.layer.cornerRadius =10;
pdfPageLable.font = [UIFontboldSystemFontOfSize:20];
pdfPageLable.textColor = [UIColorcolorWithRed:0.57fgreen:0.57fblue:0.57falpha:1.00f];
pdfPageLable.backgroundColor = [UIColorcolorWithRed:0.95fgreen:0.95fblue:0.95falpha:1.00f];
[pdfView addSubview:pdfPageLable];
self.upSwipeGestureRecognizer = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleSwipes:)];
self.downSwipeGestureRecognizer = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleSwipes:)];
self.upSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
self.downSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[pdfView addGestureRecognizer:self.upSwipeGestureRecognizer];
[pdfView addGestureRecognizer:self.downSwipeGestureRecognizer];
self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleSwipes:)];
self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleSwipes:)];
self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[pdfView addGestureRecognizer:self.leftSwipeGestureRecognizer];
[pdfView addGestureRecognizer:self.rightSwipeGestureRecognizer];
[UIViewanimateWithDuration:0.2animations:^{
pdfPageLable.alpha =1;
}];
[UIViewanimateWithDuration:2animations:^{
pdfPageLable.alpha =0;
}];
}
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
if (sender.direction ==UISwipeGestureRecognizerDirectionUp) {
[pdfView goDownPage];
}
elseif (sender.direction ==UISwipeGestureRecognizerDirectionDown) {
[pdfView goUpPage];
}
if (sender.direction ==UISwipeGestureRecognizerDirectionLeft) {
[pdfView goDownPage];
}
elseif (sender.direction ==UISwipeGestureRecognizerDirectionRight) {
[pdfView goUpPage];
}
pdfPageLable.text = [NSStringstringWithFormat:@"%d of %d",pdfView.currentPage,pdfView.totalPages];
[UIViewanimateWithDuration:0.2animations:^{
pdfPageLable.alpha =1;
}];
[UIViewanimateWithDuration:2animations:^{
pdfPageLable.alpha =0;
}];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- iOS下PDF显示
- IOS读取和显示PDF文档
- 在iOS上加载显示pdf
- iOS PDF的显示和浏览
- ios pdf解决显示乱码问题
- iOS pdf的显示和浏览
- iOS读取和显示PDF文档
- ios--PDF的显示和浏览
- 在iOS上加载显示pdf文件
- iOS读取和显示PDF文档
- iOS读取和显示PDF文档
- iOS 加载显示 Word PDF Excel
- iOS开发 pdf文档显示和预览
- linux下解决PDF乱码显示问题
- ubuntu下pdf不显示中文
- Linux下PDF文档中文显示问题解决
- ubuntu下pdf文档显示不正常
- 解决linux下pdf中文显示问题
- 大型网站架构不得不考虑的10个问题
- HDU 1008 Elevator
- 验证码倒计时
- const_cast 如何实现的
- 隐马尔科夫模型
- iOS下PDF显示
- 深入浅出RxJava——在Android中使用响应式编程
- 2016校招内推 -- 阿里巴巴前端 -- 四面面试经历
- 【第六章】 AOP 之 6.6 通知参数 ——跟我学spring3
- iOS UIDevice设备信息,传感器设置,横竖屏判断操作,手机电池操作
- Web报表工具JS开发之日期校验
- 【BZOJ3873】[Ahoi2014]拼图【分类讨论】【暴力】
- 从Google与eBay的系统架构学到的经验
- 一些常用的jQuery插件