iOS 11 PDFKit 未更新注释位置
iOS 11 PDFKit not updating annotation position
我正在构建一个应用程序来编辑 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
}
}
希望你能帮助我。
好吧,既然没人回复。我认为框架中存在错误,所以我将 post 经过一段时间的反复试验后对我有用的方法。
let initialBounds = annotation.bounds
annotation.bounds = CGRect(
origin: locationOnPage,
size: initialBounds.size)
page.removeAnnotation(annotation)
page.addAnnotation(annotation)
它不优雅,但它完成了工作
使用贝塞尔路径,整个贝塞尔路径在边界变化时移动。
PDF 的内置线型不会随着边界的变化而移动,因此必须在每次更改时设置起点和终点。
我在您的代码中添加了一行,以便在拖动时将注释中心放在您手指拖动的位置
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
}
}
我正在构建一个应用程序来编辑 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
}
}
希望你能帮助我。
好吧,既然没人回复。我认为框架中存在错误,所以我将 post 经过一段时间的反复试验后对我有用的方法。
let initialBounds = annotation.bounds
annotation.bounds = CGRect(
origin: locationOnPage,
size: initialBounds.size)
page.removeAnnotation(annotation)
page.addAnnotation(annotation)
它不优雅,但它完成了工作
使用贝塞尔路径,整个贝塞尔路径在边界变化时移动。
PDF 的内置线型不会随着边界的变化而移动,因此必须在每次更改时设置起点和终点。
我在您的代码中添加了一行,以便在拖动时将注释中心放在您手指拖动的位置
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
}
}