How to know when user swipes to next page in PDFView of PDFKit?
Asked Answered
A

2

9

In my iOS-App I'm using a PDFView with pdfView.usePageViewController(true, withViewOptions: nil).

I want to hide the PDFThumbnailView when the user swipes to another page.

I already looked inside PDFViewDelegate but there's no suitable function to use.

Agostino answered 22/5, 2018 at 13:53 Comment(0)
H
6

you can try adding observer for PDFViewPageChanged notification like this

// Add page changed listener
NotificationCenter.default.addObserver(
      self,
      selector: #selector(handlePageChange(notification:)),
      name: Notification.Name.PDFViewPageChanged,
      object: pdfView)

And then handle the page change event :

@objc private func handlePageChange(notification: Notification)
{
    // Do your stuff here like hiding PDFThumbnailView...
}

Hope it helps

Hives answered 5/7, 2018 at 9:51 Comment(1)
Any idea, on how to get the new page? :)Top
W
4

Use PDFViewPageChanged notification

Swift

// Add page change observer
NotificationCenter.default.addObserver(self,
            selector: #selector(pageDidChange(notification:)),
              name: Notification.Name.PDFViewPageChanged,
              object: nil)

 @objc private func pageDidChange(notification: Notification) {
      // pdfView is of type PDFView
      let currentPageNumber = pdfView.document?.index(for: pdfView.currentPage!)
 }

Objective C

// Add page change observer
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(pageDidChange:) name:PDFViewPageChangedNotification object:nil];

- (void) pageDidChange:(NSNotification *)notification {
    
// _pdfView is of type PDFView
    NSUInteger currentPageNumber = [_pdfView.document indexForPage:_pdfView.currentPage];
    
}
Welby answered 8/1, 2021 at 9:27 Comment(3)
PDFViewPageChagnedd notification does not work In iOS 15.2, 15.4. I could not solve it. Before 15.0 it works well.Eddy
Working fine for me. Verify your code.Welby
Apple iOS PDFKit's PDFViewPageChanged notification was not delivered because a scrollview was added on the pdfview and the methods of the scrollview's delegate were overrode. Before iOS 15.2, even though methods of the delegate were overrode, PDFKit's observer worked correctly. However, since 15.2, in this case, the observer does not work well. I removed the added scollview and added gesture recognizers instead to handle some events like tapping and panning.Eddy

© 2022 - 2024 — McMap. All rights reserved.