iOS 11 PDFKit not updating annotation position
Asked Answered
C

3

2

I'm building an app to edit PDFs on iPad.

I'm trying to implement the dragging of the annotations with a panGesture recognizer that I added to the superview of the PDFView. The problem is that the new rect bounds of the annotation gets assigned but the changes doesn't reflect on screen.

Here is my code:

@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
    }
}

Hope you can help me.

Configurationism answered 13/10, 2017 at 15:55 Comment(1)
HI, do you this sample project. I want to implement the same. Can you please share the Github link with working code?Reseda
C
3

Well, Since nobody replied. I think there is a bug in the framework, so I'll post what worked for me, after some time of trial and error.

let initialBounds = annotation.bounds
annotation.bounds = CGRect(
        origin: locationOnPage,
        size: initialBounds.size)
page.removeAnnotation(annotation)
page.addAnnotation(annotation)

It's not elegant, but it does the job

Configurationism answered 24/10, 2017 at 13:5 Comment(2)
Can you show where you added the recognizer? I need to do the same thing, but nothing is working for me. My pdfView isnt receiving the gestureArmalda
You should add it to the view behind the pdfViewConfigurationism
L
0

Using a Bezierpath the whole bezierpath moves on bounds change.

PDF's builtin line type does not get moved with changing bounds though, so have to set startPoint and endPoint at every change.

Lagniappe answered 22/12, 2017 at 22:25 Comment(0)
A
0

I added a line to your code so that when dragging, it puts the annotation center where your finger is dragging

    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
    }
}
Armalda answered 16/3, 2018 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.