如何使用UIBezierPath实现PDFKit墨迹注释

时间:2018-05-19 18:16:53

标签: ios swift pdfkit pdfview

我创建了一个PDFView。现在我只想添加一个Ink注释。因此,我会覆盖touchesBegantouchesMovedtouchesEnded方法并获取UIBezierPath。在touchesEnded我创建了PDFAnnotation并添加了此UIBezierPath。然后我将此注释添加到我的PDFView当前页面。但我无法获得正确的注释。注释的界限与我写的不同。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    path = UIBezierPath()
    path?.lineWidth = 4.0

    let touch: UITouch = touches.first!
    path?.move(to: touch.location(in: self.pdfView))
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
{
    let touch: UITouch = touches.first!
    path?.addLine(to: touch.location(in: self.pdfView))
    self.pdfView.setNeedsDisplay()
    path?.stroke()
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touch = touches.first
    path?.addLine(to: touch!.location(in: self.pdfView))
    self.pdfView.setNeedsDisplay()
    let annotation = PDFAnnotation(bounds: self.pdfView.bounds, forType: .ink, withProperties: nil)
    annotation.add(self.path!)
    if let currentPage = self.pdfView.currentPage
    {
        currentPage.addAnnotation(annotation)
    }
}

0 个答案:

没有答案