如何在无限UIScrollView中加载UIWebView而不重新加载两次?

时间:2015-10-17 13:36:16

标签: ios swift uiscrollview uiwebview infinite

使用3个UIWebView页面(0,1,2)在UISccrollView中模拟无限页面,它可以在滚动和更改内容时正常工作。

问题是当停止滚动时,currWebView会再次从旧内容重新加载新内容。如何平滑地移动页面并用闪光灯显示currWebView?

func loadPage(theVolume:Int, theChapter:Int, chapterArr:NSArray, onPage page: Int) {

    switch(page) {
    case 0:
        prevWebView.loadRequest(webViewRequest);
        break
    case 1:            
        // I try to replace the following line with: currWebView = prevWebView, it shows blank
        currWebView.loadRequest(webViewRequest);
        break
    case 2:
        nextWebView.loadRequest(webViewRequest);
        break
    default:
        break
    }
}


func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

    // moving forward
    if(scrollView.contentOffset.x > mainScrollView!.frame.size.width) {

        prevChapterArr = currChapterArr
        loadPage(volumeNum, theChapter: chapterNum, chapterArr: prevChapterArr, onPage: 0)

        var theNext = getNext(volumeNum, theChapter: chapterNum)
        volumeNum = theNext["volume"]!
        chapterNum = theNext["chapter"]!
        currChapterArr = nextChapterArr
        loadPage(volumeNum, theChapter: chapterNum, chapterArr: currChapterArr, onPage: 1)

        theNext = getNext(volumeNum, theChapter: chapterNum)
        nextVolume = theNext["volume"]!
        nextChapter = theNext["chapter"]!
        nextChapterArr = getLection(nextVolume, theChapter: nextChapter)
        loadPage(nextVolume, theChapter: nextChapter, chapterArr: nextChapterArr, onPage: 2)

    }

    // moving backward
    if(scrollView.contentOffset.x < mainScrollView!.frame.size.width) {
        // similar codes as above
    }

    // reset offset to the middle page
    self.mainScrollView!.scrollRectToVisible(CGRectMake(mainScrollView!.frame.size.width, 0, mainScrollView!.frame.size.width, mainScrollView!.frame.size.height), animated: false)

}

1 个答案:

答案 0 :(得分:0)

使用以下代码使其正常运行,无需再次请求所有网页浏览。

let tmp = prevWebView
prevWebView = currWebView
currWebView = nextWebView
nextWebView = tmp
nextWebview.loadRequest(webViewRequest)