使用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/