ios – Learn how to repair the gestureRecognizer over PDFKit breaking the paging within the new ios26 model?

0
1
ios – Learn how to repair the gestureRecognizer over PDFKit breaking the paging within the new ios26 model?


I had mission going nice, the place i wanted to do stuff with pdfs, drawing on prime them and so on. Since apple is all closed sourced i wanted to turn out to be a bit hacky. In any case, i’ve an issue because the new ios 26 replace which breaks the behaviour. I simplified the code very a lot right into a demo mission, the place you possibly can rapidly see what’s mistaken.

When swiping left to go to the subsequent web page, it does change the web page and so on within the pdf Doc, however visually nothing occurs. I’m caught on the primary web page. I dont know what to do, tried plenty of issues, however nothing works. Anybody expert sufficient to assist me out?

import UIKit
import PDFKit
import SwiftUI

class PDFViewController: UIViewController {
    
    var pdfView: PDFView!
    var gestureHandler: GestureHandler!

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        setupPDFView()
        setupGestureHandler()
        loadPDF()
    }
    
    personal func setupPDFView() {
        pdfView = PDFView(body: view.bounds)
        
        // Your actual configuration
        pdfView.autoScales = true
        pdfView.pageShadowsEnabled = false
        pdfView.backgroundColor = .white
        pdfView.displayMode = .singlePage
        
        view.addSubview(pdfView)
        
        // Setup constraints
        pdfView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            pdfView.topAnchor.constraint(equalTo: view.topAnchor),
            pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
  
  personal func setupGestureHandler() {
          gestureHandler = GestureHandler(pdfView: pdfView)
          gestureHandler.setupSwipeGestures(on: view)
      }
    
    personal func loadPDF() {
        if let path = Bundle.principal.path(forResource: "sonate12", ofType: "pdf"),
           let doc = PDFDocument(url: URL(fileURLWithPath: path)) {
            pdfView.doc = doc
        } else {
            print("Couldn't discover sonate12.pdf in bundle")
        }
    }
}


class GestureHandler {
    
    personal weak var pdfView: PDFView?
    
    init(pdfView: PDFView) {
        self.pdfView = pdfView
    }
    
    func setupSwipeGestures(on view: UIView) {
        // Left swipe - go to subsequent web page
        let leftSwipe = UISwipeGestureRecognizer(goal: self, motion: #selector(handleSwipe(_:)))
        leftSwipe.route = .left
        view.addGestureRecognizer(leftSwipe)
        
        // Proper swipe - go to earlier web page
        let rightSwipe = UISwipeGestureRecognizer(goal: self, motion: #selector(handleSwipe(_:)))
        rightSwipe.route = .proper
        view.addGestureRecognizer(rightSwipe)
    }
    
    @objc personal func handleSwipe(_ gesture: UISwipeGestureRecognizer) {
        guard let pdfView = pdfView,
              let doc = pdfView.doc,
              let currentPage = pdfView.currentPage else {
            print("🚫 No PDF view, doc, or present web page out there")
            return
        }
        
        let currentIndex = doc.index(for: currentPage)
        let totalPages = doc.pageCount
        
        print("πŸ“„ Present state: Web page (currentIndex + 1) of (totalPages)")
        print("πŸ‘† Swipe route: (gesture.route == .left ? "LEFT (subsequent)" : "RIGHT (earlier)")")
        
        swap gesture.route {
        case .left:
            // Subsequent web page
            guard currentIndex < doc.pageCount - 1 else {
                print("🚫 Already on final web page ((currentIndex + 1)), can not go ahead")
                return
            }
            
            let nextPage = doc.web page(at: currentIndex + 1)
            if let web page = nextPage {
                print("➑️ Going to web page (currentIndex + 2)")
                pdfView.go(to: web page)
              pdfView.setNeedsDisplay()
              pdfView.layoutIfNeeded()
                // Test if navigation truly labored
                DispatchQueue.principal.asyncAfter(deadline: .now() + 0.1) {
                    if let newCurrentPage = pdfView.currentPage {
                        let newIndex = doc.index(for: newCurrentPage)
                        print("βœ… Navigation consequence: Now on web page (newIndex + 1)")
                        if newIndex == currentIndex {
                            print("⚠️ WARNING: Web page did not change visually!")
                        }
                    }
                }
            } else {
                print("🚫 Couldn't get subsequent web page object")
            }
            
        case .proper:
            // Earlier web page
            guard currentIndex > 0 else {
                print("🚫 Already on first web page (1), can not return")
                return
            }
            
            let previousPage = doc.web page(at: currentIndex - 1)
            if let web page = previousPage {
                print("⬅️ Going to web page (currentIndex)")
                pdfView.go(to: web page)
              pdfView.setNeedsDisplay()
              pdfView.layoutIfNeeded()
              let bounds = pdfView.bounds
              pdfView.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.width + 0.01, peak: bounds.peak)
              pdfView.bounds = bounds

                
                // Test if navigation truly labored
                DispatchQueue.principal.asyncAfter(deadline: .now() + 0.1) {
                    if let newCurrentPage = pdfView.currentPage {
                        let newIndex = doc.index(for: newCurrentPage)
                        print("βœ… Navigation consequence: Now on web page (newIndex + 1)")
                        if newIndex == currentIndex {
                            print("⚠️ WARNING: Web page did not change visually!")
                        }
                    }
                }
            } else {
                print("🚫 Couldn't get earlier web page object")
            }
            
        default:
            print("πŸ€·β€β™‚οΈ Unknown swipe route")
            break
        }
    }
}


struct PDFViewerRepresentable: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> PDFViewController {
        return PDFViewController()
    }
    
    func updateUIViewController(_ uiViewController: PDFViewController, context: Context) {
        // No updates wanted
    }
}

You may take a look at the code right here as effectively: https://github.com/vallezw/swift-bug-ios26

LEAVE A REPLY

Please enter your comment!
Please enter your name here