一種可能的解決方案是在顯示之前下載並緩存所有頁面。
看到這個答案下載HTML網頁並寫在本地。
https://stackoverflow.com/q/5607132/300972
創建到這些頁的引用數組,然後將它們加載到相應的UIWebView
的在scrollViewDidEndDecelerating
。
更新:
使用上面的鏈接,它看起來像這樣。請注意,我沒有測試過這個。
// NSArray *urls;
// NSMutableArray *filePaths;
- (void)viewDidLoad
{
[super viewDidLoad];
urls = [NSArray arrayWithObjects:@"http://www.url1.com", @"http://www.url2.com", @"http://www.url3.com", nil];
for (int i=0; i<[urls count]; i++)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@.html", [paths objectAtIndex:0], [urls objectAtIndex:i]];
NSURL *url = [NSURL URLWithString:[urls objectAtIndex:i]];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// Do some calculation to find index of webpage, this depends on
// the size of the scrollview and the size of the webview.
int index = ...;
NSString *filePath = [NSString stringWithFormat:@"%@/%@.html", [paths objectAtIndex:0], [urls objectAtIndex:i]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
}
更新了樣品。 –