iphone - ZoomingPDFViewer无法在启用分页的UIScrollViewer中缩放

标签 iphone ios

我试图显示水平放置的单页PDF,以便可以通过向左或向右滑动来浏览页面。当然,我需要缩放页面。

我在启用分页的容器UIWebView中使用UIScrollViewer来实现它。 UIWebView用作PDF查看器,并且UIScrollView处理页面更改。但是这种实现不是一个好方法,它使用了过多的内存,因此我决定使用Apple的ZoomingPDFViewer而不是UIWebView

我的带有UIWebView的原始代码如下所示:

for (int i=0; i<n; i++)
{
    CGRect frame;
    frame.origin.y = 0;
    frame.origin.x = self.scrollviewer.frame.size.width * i;
    frame.size = self.scrollviewer.frame.size;

    UIWebView *pageViewer = [[UIWebView alloc] initWithFrame:frame];
    pageViewer.scalesPageToFit = TRUE;
    pageViewer.multipleTouchEnabled = TRUE;

    [self.scrollviewer addSubview:pageViewer];

    NSURL *url = [NSURL URLWithString:address];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    [pageViewer loadRequest:req];
}

self.scrollviewer.contentSize = CGSizeMake(nPages * self.scrollviewer.frame.size.width, self.scrollviewer.frame.size.height);

这很完美,但是当我有25页以上时,它很慢,并且Apple不建议在UIWebViewUIScrollView中使用UITableView,因为触摸事件可能会产生意想不到的结果。

因此,我想将UIWebView更改为Apple示例中给出的ZoomingPDFViewer。我的新代码ZoomingPDFViewer看起来像这样:
for (int i=0; i<n; i++)
{
    CGRect frame;
    frame.origin.y = 0;
    frame.origin.x = self.scrollviewer.frame.size.width * i;
    frame.size = self.scrollviewer.frame.size;

    PDFScrollView *pageViewer = [[PDFScrollView alloc] initWithFrame:frame];

    [self.scrollviewer addSubview:pageViewer];

    NSURL *url = [NSURL URLWithString:address];
    CGPDFDocumentRef PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);

    CGPDFPageRef PDFPage = CGPDFDocumentGetPage(PDFDocument, 1);
    [(PDFScrollView *)pageViewer setPDFPage:PDFPage];

    CGPDFDocumentRelease(PDFDocument);
}

self.scrollviewer.contentSize = CGSizeMake(nPages * self.scrollviewer.frame.size.width, self.scrollviewer.frame.size.height);

此代码显示了我的PDF页面,可以在页面之间滑动,但是无法放大页面。

我不知道该怎么办。你能帮助我吗?

最佳答案

我找到了解决方案。

问题是,当您以编程方式创建UIScrollView时,默认情况下会设置minimumZoomScalemaximumZoomScale 1.0。所以实际上我的代码在工作,但是我不知道它在工作,因为它没有放大或缩小。

所以我在我的代码中添加了两行,就可以了!这是我的代码中的更改

PDFScrollView *pageViewer = [[PDFScrollView alloc] initWithFrame:frame];
pageViewer.minimumZoomScale = 0.2;
pageViewer.maximumZoomScale = 5.0;

[self.scrollviewer addSubview:pageViewer];

当我插入两行时,它可以工作,但是我可以无限放大或缩小。我想以最大比例5缩放。因此,我更改了文件scrollViewDidEndZooming中给出的PDFScrollView.m方法

Apple的ZoomingPDFViewer随附的默认代码如下所示:
// Set the new scale factor for the TiledPDFView.
_PDFScale *= scale;

// Calculate the new frame for the new TiledPDFView.
CGRect pageRect = CGPDFPageGetBoxRect(_PDFPage, kCGPDFMediaBox);
pageRect.size = CGSizeMake(pageRect.size.width*_PDFScale, pageRect.size.height*_PDFScale);

更改后,它看起来像:
// Set the new scale factor for the TiledPDFView.
_PDFScale *= scale;

self.minimumZoomScale *= 1 / scale;
self.maximumZoomScale *= 1 / scale;

// Calculate the new frame for the new TiledPDFView.
CGRect pageRect = CGPDFPageGetBoxRect(_PDFPage, kCGPDFMediaBox);
pageRect.size = CGSizeMake(pageRect.size.width*_PDFScale, pageRect.size.height*_PDFScale);

现在可以了。

关于iphone - ZoomingPDFViewer无法在启用分页的UIScrollViewer中缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15627107/

相关文章:

iOS - 获取 ARP 表

iphone - cocos2d : How to set a timer

iphone - 在 UIPinchGestureRecognizer 中获取触发手势的手指数量?

iPhone Icon@2x.png 未在 Retina 显示屏中显示

iphone - 如何从 NSObject 类中调用添加 View

ios - 如何测量 iOS 中的延迟

ios - Firebase FCM主题-可以是已订阅用户的用户ID吗?

ios - 在 UILabel 上使用 NSMutableAttributedString 时表情符号丢失

ios - CoreData 用户定义的数据库结构?

ios - 当应用程序在后台使用 Sinch iOS SDK 时无法处理来电推送通知