Estoy cargando un PDF en la vista PDF usando la biblioteca del kit de PDF. Agregué una vista personalizada (igual que la anotación de PDF) en la vista de pdf, y permito que los usuarios muevan/arrastren esa vista personalizada en la vista de pdf (dentro de la vista de pdf/vista de contenedor) usando UIPanGestureRecognizer. Aquí hay un gif,
Si ves este gif, hay un problema. Esa vista personalizada va fuera de la página pdf. Quiero restringirlo. La vista personalizada debe moverse/arrastrarse solo dentro de la página pdf. ¿Cómo puedo arreglar esto? ¿Hay una solución para eso?
Aquí está el proyecto de muestra del enlace y todo el código: https://drive.google.com/file/d/1Ilhd8gp4AAxB_Q9G9swFbe4KQUHbpyGs/view?usp=sharing
Aquí hay una muestra de código del proyecto,
override func didMoveToSuperview() { addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(pan))) } @objc func pan(_ gesture: UIPanGestureRecognizer) { translate(gesture.translation(in: self)) gesture.setTranslation(.zero, in: self) setNeedsDisplay() print("Frames after moving : \(frame)") }y código usado como una extensión
extension CGPoint { static func +(lhs: CGPoint, rhs: CGPoint) -> CGPoint { .init(x: lhs.x + rhs.x, y: lhs.y + rhs.y) } static func +=(lhs: inout CGPoint, rhs: CGPoint) { lhs.x += rhs.x lhs.y += rhs.y } } extension UIView { func translate(_ translation: CGPoint) { let destination = center + translation let minX = frame.width/2 let minY = frame.height/2 let maxX = superview!.frame.width-minX let maxY = superview!.frame.height-minY center = CGPoint( x: min(maxX, max(minX, destination.x)), y: min(maxY ,max(minY, destination.y))) } }Código - Obtener alto y ancho de página PDF
let page = pdfDocument.page(at: 0) let pageRect = page?.bounds(for: .mediaBox) print("pdf page width =", (pageRect?.size.width)!, "pdf page height =", (pageRect?.size.height)!)