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

标签 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)

}

最佳答案

使用以下代码即可使其正常工作,无需再次请求所有 WebView 。

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

关于ios - 如何在无限 UIScrollView 中加载 UIWebView 而无需重新加载两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33187249/

相关文章:

ios - 如何使用 Realm 确定一对一关系的反向关系?

ios - 如何检查是否给出了正确的链接

ios - “NSInvalidArgumentException”: attempt to scroll to invalid index path

ios - 如何从框架中调配 AppDelegate 的 didReceiveRemoteNotification

ios - 使用带有自动布局的 UIScrollView

swift - RxSwift 使用数据收集

ios - Xcode 11.4 编译错误 'Missing package product <package name>'

swift - Firebase 数据库中的多用户聊天室数据结构

ios - 在接收触摸的 UIView 下缩放 UIScrollView

swift - 在 xcode swift uiscrollview height destination 中需要帮助