About UIWebView loading a Web PDF to achieve page turn effects

Source: Internet
Author: User

Well, first of all, the PDF that loads the network is not local.

Creating a new view is primarily used for looping the rendering of a single page of PDF content

The. h override Init method is used to customize the receive Docref file and incoming page pages

-(Instancetype) initWithFrame: (CGRect) frame documentref: (cgpdfdocumentref) docref andpagenum: (int) page;

. m page implementation method and requires attribute method

Cgpdfdocumentref Documentref; This is the doc file type that the PDF receives

Nsinteger Pagenum; Receives the current page

-(Instancetype) initWithFrame: (CGRect) frame documentref: (cgpdfdocumentref) docref andpagenum: (int) page{

self = [super Initwithframe:frame];

if (self) {

Documentref= Docref;

_pagenum= page;

self.backgroundcolor= [Uicolor Whitecolor];

}

return self;

}

And then, because it's a circular render draw, you need to override DrawRect Override this method to perform a redraw task

The DrawRect call is called after the Controller->loadview,,controller->viewdidload two method.

-(void) DrawRect: (cgrect) Rect {

[Self Drawpdfincontext:uigraphicsgetcurrentcontext ()];//passing the current context environment new method

}

Okay, the most important part is coming.

-(void) Drawpdfincontext: (cgcontextref) Context {

Adjust the position of a graphic

//Quartz坐标系和UIView坐标系不一样所致,调整坐标系,使pdf正立

CGCONTEXTTRANSLATECTM (Context,0.0,self.frame.size.height);

The graphic is displayed in a positive state

CGCONTEXTSCALECTM (context,1.0,-1.0);

/获取指定页的pdf文档

Cgpdfpageref pageRef =cgpdfdocumentgetpage (documentref,_pagenum);

Document the current drawing environment to prevent multiple painting

Cgcontextsavegstate (context);

//创建一个仿射变换,该变换基于将PDF页的BOX映射到指定的矩形中。

Cgaffinetransform pdftransform =cgpdfpagegetdrawingtransform (pageref,kcgpdfcropbox,self.bounds,0,true);

CGCONTEXTCONCATCTM (context, pdftransform);

//将pdf绘制到上下文中

Cgcontextdrawpdfpage (context, PAGEREF);

//将pdf保存

Cgcontextrestoregstate (context);

}

Okay, here's how to customize the view implementation. Then go back to the VC page

Mainly in the viewdidload inside. M

I'm going to start here.

Flip page We can think of a lot of things like Uipageviewcontroller Uicollectionview and so here I'm mainly talking about Uipageviewcontroller

@property (Nonatomic,strong) Uipageviewcontroller *PAGEVC;

@property (Nonatomic,strong) Nsmutablearray *pdfarr;

You all know that?

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);//This position is mainly to get the number of PDF pages;

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;//returns the data obtained

}else{

return NULL; If you do not get the data, then return null, of course, you can add some print logs here, so you find the problem

}

}

-(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];

}

About UIWebView loading a Web PDF to achieve page turn effects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.