如何在 WKWebView 中使用 PDF 一次滚动一个视图的高度(不是页面)(Swift)

How to scroll one view's height (not page) at a time with a PDF in WKWebView (Swift)

我有一组 PDF,有些是一页,有些是两页。我想查看 PDF 的下一个可见屏幕高度值,或者如果 PDF 的剩余高度小于 webView 的高度,则滚动到 PDF 的底部。到目前为止,我只能让 webView 滚动到底部。这是我的功能......我错过了什么?

func scrollDown() {
    let scrollView = webview.scrollView
    let contentSize = scrollView.contentSize
    let contentOffset = scrollView.contentOffset
    let frameSize = webview.frame.size

    if scrollView.contentOffset.y <= 0 {
        // Scroll one view's height at a time
        // If the contentSize - offset.y is greater than the frame's height
        if (contentSize.height - contentOffset.y) > frameSize.height {
            scrollView.setContentOffset(CGPoint(x: 0, y: contentOffset.y + (contentSize.height - frameSize.height)), animated: true)
        } else {
            scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
        }
    }
}

以防其他人需要这个。吸取的教训是将其画在纸上以了解发生了什么...

func scrollDown() {
    let scrollView = webview.scrollView
    let contentSize = scrollView.contentSize
    let contentOffset = scrollView.contentOffset
    let frameSize = webview.frame.size
    let frameHeight = frameSize.height

    // Next view's height
    let heightOffset = frameSize.height + contentOffset.y
    let offsetToBottom = contentSize.height - frameSize.height

    if contentOffset.y + frameHeight > contentSize.height - frameHeight {
        scrollView.setContentOffset(CGPoint(x: 0, y: offsetToBottom), animated: true)
        print("Should be scrolling to bottom")
    } else {
        scrollView.setContentOffset(CGPoint(x: 0, y: heightOffset), animated: true)
        print("Should be scrolling by one page")
    }
}