iOS 11 PDFKit未更新注释位置



我正在构建一个在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
}
}

希望你能帮助我。

好吧,因为没有人回复。我认为框架中有一个错误,所以经过一段时间的尝试和错误,我会发布对我有用的东西。

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

它并不优雅,但它完成了的工作

使用贝塞尔路径,整个贝塞尔路径会随着边界的变化而移动。

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

我在代码中添加了一行,这样在拖动时,它会将注释中心放在手指拖动的位置

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
}
}

最新更新