簡體   English   中英

如何獲取UIWebView頁碼

[英]How to get UIWebView page number

我使用以下代碼對UIWebView進行了分頁。

self.webView.paginationMode = UIWebPaginationModeLeftToRight;
self.webView.paginationBreakingMode = UIWebPaginationBreakingModePage;
self.webView.scrollView.pagingEnabled = YES;

現在,我想知道它創建的頁面總數。 IOS7文檔說UIWebView有一個名為pageCount的屬性。 我試過了,但它總是返回1。

分頁后如何獲取它創建的頁面數?

謝謝。

如果只需要頁數,則可以使用CoreGraphics

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:@"your path"]);
int pageCount = CGPDFDocumentGetNumberOfPages(pdf);
CGPDFDocumentRelease(pdf);

如果要計算HTML內容的頁面,則需要設置委托

self.webView.delegate = self;

並實現UIWebViewDelegate方法

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSUInteger count1 = webView.pageCount; // for iOS7 only!

    NSUInteger count2 = webView.scrollView.contentSize.width / webView.scrollView.frame.size.width; // for horizontal paging

    NSUInteger count3 = webView.scrollView.contentSize.height / webView.scrollView.frame.size.height; // for vertical paging
}

請注意,只有在完全渲染請求的HTML頁面之后才能獲取pageCount。

UIWebView是UIScrollView的子類。 您可以為滾動視圖委托方法檢測UIWebView頁面更改事件:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

每當webView滾動時,都會調用此委托。 自己維護頁碼。 您可以使用scrollView.contentOffset.x和scrollView.contentOffset.y確定滾動方向。

您可以在此處找到更多幫助。

編輯我們可以從contentoffset找到scrollview的頁碼。 為webView嘗試類似的操作。 int頁面= scrollView.contentOffset.x / scrollView.frame.size.width;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM