iOS 11 PDFKit не обновляет позицию аннотации
Я создаю приложение для редактирования PDF-файлов на iPad.
Я пытаюсь реализовать перетаскивание аннотаций с помощью распознавателя panGesture, который я добавил в суперпредставление PDFView. Проблема в том, что новые прямоугольные границы аннотации назначаются, но изменения не отображаются на экране.
Вот мой код:
@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
}
}
Надеюсь, ты сможешь мне помочь.
3 ответа
Ну, так как никто не ответил. Я думаю, что во фреймворке есть ошибка, поэтому я опубликую, что сработало для меня, после некоторого времени проб и ошибок.
let initialBounds = annotation.bounds
annotation.bounds = CGRect(
origin: locationOnPage,
size: initialBounds.size)
page.removeAnnotation(annotation)
page.addAnnotation(annotation)
Это не элегантно, но делает работу
Я добавил строку в ваш код, чтобы при перетаскивании она помещала центр аннотации, куда тянет ваш палец
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
}
}
Используя Безье-путь, весь безье-путь перемещается по изменению границ.
Встроенный тип линий PDF не перемещается с изменением границ, поэтому необходимо устанавливать startPoint и endPoint при каждом изменении.