I wish to drag PDFAnnotation when the pdf view is zoomed in or zoomed out, At the moment I can capable of drag Pin when the pdf just isn’t zoomed in. Right here is my code which is at present working. please counsel any legitimate reply and thanks in your time.
import UIKit
import PDFKit
class ImageStampAnnotation: PDFAnnotation {
var picture: UIImage!
// A customized init that units the kind to Stamp on default and assigns our Picture variable
init(with picture: UIImage!, forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
tremendous.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp, withProperties: properties)
self.picture = picture
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been applied")
}
override func draw(with field: PDFDisplayBox, in context: CGContext) {
// Get the CGImage of our picture
guard let cgImage = self.picture.cgImage else { return }
// Draw our CGImage within the context of our PDFAnnotation bounds
context.draw(cgImage, in: self.bounds)
}
}
class DragVC: UIViewController {
@IBOutlet weak var pdfContainerView: PDFView!
var currentlySelectedAnnotation: PDFAnnotation?
var signatureImage: UIImage?
var annotations = [ImageStampAnnotation]()
override func viewDidLoad() {
tremendous.viewDidLoad()
self.title = "PDF Viewer"
self.signatureImage = UIImage(named: "Inexperienced-png")
setupPdfView()
}
override func viewDidAppear(_ animated: Bool) {
tremendous.viewDidAppear(animated)
guard let signatureImage = signatureImage, let web page = pdfContainerView.currentPage else { return }
let pageBounds = web page.bounds(for: .cropBox)
let imageBounds = CGRect(x: pageBounds.midX, y: pageBounds.midY, width: 120, top: 120)
let imageBounds1 = CGRect(x: pageBounds.midX + 100, y: pageBounds.midY + 100, width: 120, top: 120)
let imageBounds2 = CGRect(x: pageBounds.midX + 150, y: pageBounds.midY + 150 , width: 120, top: 120)
let imageStamp = ImageStampAnnotation(with: signatureImage, forBounds: imageBounds, withProperties: nil)
imageStamp.fieldName = "1"
let imageStamp1 = ImageStampAnnotation(with: signatureImage, forBounds: imageBounds1, withProperties: nil)
imageStamp1.fieldName = "2"
let imageStamp2 = ImageStampAnnotation(with: signatureImage, forBounds: imageBounds2, withProperties: nil)
imageStamp2.fieldName = "3"
web page.addAnnotation(imageStamp)
web page.addAnnotation(imageStamp1)
web page.addAnnotation(imageStamp2)
annotations.append(imageStamp)
annotations.append(imageStamp1)
annotations.append(imageStamp2)
}
func setupPdfView() {
// Obtain easy pdf doc
if let documentURL = Bundle.primary.url(forResource: "example1", withExtension: "pdf"),
let information = strive? Information(contentsOf: documentURL),
let doc = PDFDocument(information: information) {
// Set doc to the view, middle it, and set background shade
pdfContainerView.doc = doc
pdfContainerView.autoScales = true
pdfContainerView.backgroundColor = UIColor.lightGray
let panAnnotationGesture = UIPanGestureRecognizer(goal: self, motion: #selector(handlePanGesture(_:)))
pdfContainerView.addGestureRecognizer(panAnnotationGesture)
let web page = doc.web page(at: 0)
let pageRect = web page?.bounds(for: .mediaBox)
}
}
@objc func handlePanGesture(_ sender: UIPanGestureRecognizer) {
let touchLocation = sender.location(in: pdfContainerView)
guard let web page = pdfContainerView.web page(for: touchLocation, nearest: true)
else {
return
}
let locationOnPage = pdfContainerView.convert(touchLocation, to: web page)
change sender.state {
case .started:
guard let annotation = web page.annotation(at: locationOnPage) else {
return
}
if annotation.isKind(of: ImageStampAnnotation.self) {
currentlySelectedAnnotation = annotation
}
case .modified:
guard let annotation = currentlySelectedAnnotation else {
return
}
let initialBounds = annotation.bounds
if let index = annotations.firstIndex(of: annotation as! ImageStampAnnotation) {
annotations[index].bounds = CGRect(x: locationOnPage.x, y: locationOnPage.y , width: initialBounds.width, top: initialBounds.top)
}
let translation = sender.translation(in: self.pdfContainerView)
let newPosition = CGPoint(x: annotation.bounds.origin.x + translation.x,
y: annotation.bounds.origin.y + translation.y)
annotation.bounds.origin = newPosition
sender.setTranslation(.zero, in: self.pdfContainerView)
case .ended, .cancelled, .failed:
currentlySelectedAnnotation = nil
default:
break
}
}
//
}