I want to drag PDFAnnotation when the pdf view is zoomed in or zoomed out, I can drag Pin when the pdf is not zoomed in. Here is my code which is currently working.
import UIKit
import PDFKit
class ImageStampAnnotation: PDFAnnotation {
var image: UIImage!
// A custom init that sets the type to Stamp on default and assigns our Image variable
init(with image: UIImage!, forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
super.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp, withProperties: properties)
self.image = image
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(with box: PDFDisplayBox, in context: CGContext) {
// Get the CGImage of our image
guard let cgImage = self.image.cgImage else { return }
// Draw our CGImage in 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() {
super.viewDidLoad()
self.title = "PDF Viewer"
self.signatureImage = UIImage(named: "Green-png")
setupPdfView()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
guard let signatureImage = signatureImage, let page = pdfContainerView.currentPage else { return }
let pageBounds = page.bounds(for: .cropBox)
let imageBounds = CGRect(x: pageBounds.midX, y: pageBounds.midY, width: 120, height: 120)
let imageBounds1 = CGRect(x: pageBounds.midX + 100, y: pageBounds.midY + 100, width: 120, height: 120)
let imageBounds2 = CGRect(x: pageBounds.midX + 150, y: pageBounds.midY + 150 , width: 120, height: 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"
page.addAnnotation(imageStamp)
page.addAnnotation(imageStamp1)
page.addAnnotation(imageStamp2)
annotations.append(imageStamp)
annotations.append(imageStamp1)
annotations.append(imageStamp2)
}
func setupPdfView() {
// Download simple pdf document
if let documentURL = Bundle.main.url(forResource: "example1", withExtension: "pdf"),
let data = try? Data(contentsOf: documentURL),
let document = PDFDocument(data: data) {
// Set document to the view, center it, and set background color
pdfContainerView.document = document
pdfContainerView.autoScales = true
pdfContainerView.backgroundColor = UIColor.lightGray
let panAnnotationGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
pdfContainerView.addGestureRecognizer(panAnnotationGesture)
}
}
@objc func handlePanGesture(_ sender: UIPanGestureRecognizer) {
let touchLocation = sender.location(in: pdfContainerView)
guard let page = pdfContainerView.page(for: touchLocation, nearest: true)
else {
return
}
let locationOnPage = pdfContainerView.convert(touchLocation, to: page)
switch sender.state {
case .began:
guard let annotation = page.annotation(at: locationOnPage) else {
return
}
if annotation.isKind(of: ImageStampAnnotation.self) {
currentlySelectedAnnotation = annotation
}
case .changed:
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, height: initialBounds.height)
}
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
}
}
//
}