旋转BezierPath绕x轴的PDF注释斯威夫特4的iOS

问题描述 投票:2回答:1

我试图用UIBezierPath使用类型.ink在我的应用程序注释的PDF文件。我已经包含下面的相关代码片段(可以增加整个样本,但问题是只能用旋转路径)。问题是,当我将此路径,它围绕旋转x轴所以基本上是上下翻转180度。我想能够使其显示作为最初打算旋转此路径绕x轴旋转180度。我看到示例绕z轴而是围绕x轴无旋转。任何帮助将不胜感激!

            let rect = CGRect(x: 110, y: 100, width: 400, height: 300)

            let annotation = PDFAnnotation(bounds: rect, forType: .ink, withProperties: nil)
            annotation.backgroundColor = .blue

            path.apply(CGAffineTransform(scaleX: 0.2, y: 0.2))
            annotation.add(path)

            // Add annotation to the first page
            page.addAnnotation(annotation)

            pdfView?.document?.page(at: 0)?.addAnnotation(annotation)
ios swift rotation uibezierpath cgaffinetransform
1个回答
2
投票

实际上,我是能够使用下面的规模来解决这个问题,翻译转换:

let rect = CGRect(x: 110, y: 100, width: 400, height: 300)

let annotation = PDFAnnotation(bounds: rect, forType: .ink, withProperties: nil)
annotation.backgroundColor = .blue

// OVER HERE 🙂
path.apply(CGAffineTransform(scaleX: 1.0, y: -1.0))
path.apply(CGAffineTransform(translationX: 0, y: rect.size.height))

annotation.add(path)

// Add annotation to the first page
page.addAnnotation(annotation)

pdfView?.document?.page(at: 0)?.addAnnotation(annotation)

该解决方案从Apple Developer Documentation example启发。

因为PDFKit坐标系统采用底部/左为原点,与x轴线行进,这是一个有点棘手左到右,Y轴去底部到顶部。这是违背了原点:顶/左,X:左到右和y:顶部到底部的模式通常在iOS上遇到。但是,这是我们正在做的事情:

  • CGAffineTransform(scaleX: 1.0, y: -1.0) - 缩放y坐标到-1.0使你的路径围绕倒装x轴180度(目视轴作为rect的底线表示)。这意味着现在path低于rect,这可能给你的印象,它已经消失了(你甚至不会通过捕获视图层次找到它,因为它会告诉你整个PDFView作为一个UIView的成分,这可能是可能无法驱动你疯了)。
  • CGAffineTransform(translationX: 0, y: rect.size.height) - 现在的path是在rect的底部(但实际上在根据PDFKit坐标系中的“顶部”),我们需要把它放回可见区域。这就是为什么我们需要应用平移变换移动path向上(或向下 - 再次感谢PDFKit)到rect

希望这可以帮助!干杯

© www.soinside.com 2019 - 2024. All rights reserved.