2017-02-16 10 views
0

我有一個PDF集合,有些是一個頁面,有些是兩個。我希望看到PDF的下一個可見屏幕高度值,或者如果PDF的剩餘高度小於webView的高度,請滾動到PDF的底部。到目前爲止,我只能讓webView滾動到底部。這是我的功能...我錯過了什麼?如何使用WKWebView(Swift)以PDF格式一次滾動一個視圖的高度(不是頁面)

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

回答

0

萬一其他人需要這個。吸取的教訓是繪製在紙上以瞭解正在發生什麼...

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") 
    } 
} 
相關問題