7

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.

heyfrank
  • 5,291
  • 3
  • 32
  • 46

2 Answers2

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

heyfrank
  • 5,291
  • 3
  • 32
  • 46
Adrian
  • 324
  • 2
  • 10
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];
    
}
Sreekuttan
  • 1,579
  • 13
  • 19
  • PDFViewPageChagnedd notification does not work In iOS 15.2, 15.4. I could not solve it. Before 15.0 it works well. – alones Aug 17 '22 at 04:44
  • Working fine for me. Verify your code. – Sreekuttan Aug 17 '22 at 07:50
  • 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. – alones Aug 31 '22 at 04:40