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


.VC

#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



0 0
原创粉丝点击