【问题标题】:iOS 11 PDFKit not updating annotation positioniOS 11 PDFKit 不更新注释位置
【发布时间】:2018-03-25 19:34:47
【问题描述】:

我正在构建一个在 iPad 上编辑 PDF 的应用程序。

我正在尝试使用我添加到 PDFView 的超级视图的 panGesture 识别器来实现注释的拖动。问题是注释的新矩形边界已分配,但更改并未反映在屏幕上。

这是我的代码:

@objc func handlePanGesture(panGesture: UIPanGestureRecognizer) {
    let touchLocation = panGesture.location(in: pdfView)

    guard let page = pdfView.page(for: touchLocation, nearest: true) else {
        return
    }
    let locationOnPage = pdfView.convert(touchLocation, to: page)

    switch panGesture.state {
    case .began:

           guard let annotation = page.annotation(at: locationOnPage) else {
                return
            }
            currentlySelectedAnnotation = annotation
    case .changed:

        guard let annotation = currentlySelectedAnnotation else {
            return
        }
        let initialBounds = annotation.bounds
        annotation.bounds = CGRect(origin: locationOnPage,
                                   size: initialBounds.size)

        print("move to \(locationOnPage)")
    case .ended, .cancelled, .failed:
        break
    default:
        break
    }
}

希望你能帮助我。

【问题讨论】:

    标签: swift ios11 pdfkit


    【解决方案1】:

    好吧,既然没有人回复。我认为框架中存在错误,因此经过一段时间的反复试验后,我将发布对我有用的内容。

    let initialBounds = annotation.bounds
    annotation.bounds = CGRect(
            origin: locationOnPage,
            size: initialBounds.size)
    page.removeAnnotation(annotation)
    page.addAnnotation(annotation)
    

    它并不优雅,但它可以完成工作

    【讨论】:

    • 你能显示你在哪里添加了识别器吗?我需要做同样的事情,但没有什么对我有用。我的 pdfView 没有收到手势
    • 你应该把它添加到pdfView后面的视图中
    【解决方案2】:

    使用 Bezierpath,整个 bezierpath 会随着边界变化而移动。

    PDF 的内置线型不会随着边界的变化而移动,因此必须在每次更改时设置 startPoint 和 endPoint。

    【讨论】:

      【解决方案3】:

      我在您的代码中添加了一行,以便在拖动时将注释中心放在您手指拖动的位置

          case .changed:
      
              guard let annotation = currentlySelectedAnnotation else {
                  return
              }
              let initialBounds = annotation.bounds
              // Set the center of the annotation to the spot of our finger
              annotation.bounds = CGRect(x: locationOnPage.x - (initialBounds.width / 2), y: locationOnPage.y - (initialBounds.height / 2), width: initialBounds.width, height: initialBounds.height)
      
      
              print("move to \(locationOnPage)")
          case .ended, .cancelled, .failed:
              currentlySelectedAnnotation = nil
          default:
              break
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-08-02
        • 1970-01-01
        • 2018-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-18
        • 2013-04-17
        • 2015-01-21
        相关资源
        最近更新 更多