0


    我正在尝试在我的资源包中由 CGContextRef 生成的 pdf 文档上绘制触摸(鼠标点击)。我从 Apple zoomingpdfviewer开始, 在那里我有一个 UIScrollView 和 Pdf 是由CGContextDrawPDFPage(context, pdfPage);

我还在 scrollView 上添加了一个 CATiledLayer,并且每次都在 layoutSubviews 中绘制它 - 当 UIScrollView 被缩放时。我有点困惑,我应该在 scrollView CGContext 或 TileLayers 上绘制鼠标点。

--> 继续前进,我想在用户点击的 pdf 文档上添加一个矩形/圆圈/点。
  刚开始我是:
CGContextMoveToPoint(context, 5,5); CGContextSetLineWidth(context, 12.0f); CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]); CGContextAddRect(context, CGRectMake(50, 50, 20, 50)); CGContextAddLineToPoint(context, 25, 5); CGContextStrokePath(context);

将其绘制到当前上下文。同样,我计划在用户点击时进行绘制。
  但是当我缩放并重新绘制 TitledLayer 以匹配缩放内容时,这些绘制的矩形/圆形消失了。
如果我没记错,那么 CATiledLayer 被绘制在 currentContext rects/ circle 之上。
     
   最终功能与添加了 Tile Layers 的 Maps App 非常相似,但即使在地图缩放后,放置的点也完全位于同一位置。
     
    在看到许多诸如在 scrollViewpdf iOS 查看器上绘图之类的帖子后,非常迷茫。
    有谁知道我如何在 pdf 文档上绘制几何图形(矩形/点)并保持它们的位置完全相同,即使 PdfView 放大和缩小?我应该将 pdf 转换为图像还是任何其他方式来做到这一点?

4

2 回答 2

0

进一步添加(如果有人到达这里),替换 Apple 代码
  TiledPdfView *titledPDFView = [[TiledPdfView alloc] initWithFrame:pageRect scale:pdfScale]; [titledPDFView setPage:pdfPage]; [self addSubview:titledPdfView]; [titledPDFView setNeedsDisplay]; [self bringSubviewToFront:titledPdfView]; self.titledPdfView = titledPDFView;
  With :

titledPdfView = [[TiledPdfView alloc] initWithFrame:pageRect scale:pdfScale];
[titledPdfView setPage:pdfPage];
[self addSubview:titledPdfView];
[self bringSubviewToFront:titledPdfView];

我不知道他们为什么要添加这样的视图(成员对象到类对象),这限制了
  -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
缩放之前的存在。我试过 setNeedsDisplay 没有效果,然后替换它可以工作。
  特别是。如果您想在 TileLayer 上绘制注释并且什么都没有出现,这很烦人。希望这可以帮助!!

于 2013-12-26T12:04:33.687 回答
0

在CoreAnimation,CALayer上搜索并尝试了一些东西后,我想出了:如果你想在可缩放的PDF查看器上添加任何东西,

1)添加一个UITapGestureRecognizer来处理单点触摸

UITapGestureRecognizer *singleTapGestureRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapOnScrollView:)];
    singleTapGestureRecognizer.numberOfTapsRequired=1;
    singleTapGestureRecognizer.delegate=self;
    [self addGestureRecognizer:singleTapGestureRecognizer];
    [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer]; // added if you have a UITapGestureRecognizer for double taps

2) 只需将新视图(圆形、矩形任何几何图形)添加为 Tiled PDF 视图上的图层

[self.titledPdfView.layer addSublayer:view1.layer];

您可以在其中子类化几何视图以使用 CGContext currentContext 进行绘制。这会在 pdf 放大/缩小时自动重新定位图层

我试图通过在 drawLayer:(CALayer*)layer inContext:(CGContextRef)context of titledPdfView 类上绘制上下文来添加视图,这给出了位置错误

如果要绘制可调整大小的 TextView、arrow、Line,还可以实现 TouchesMoved、TouchesEnded 方法。

我希望这对需要在 Apple 的 ZoomingPDFViewer 上绘制自定义对象的人有所帮助。

于 2013-12-17T11:36:00.170 回答