我有一堆静态html内容的webview,我将其作为页面放在滚动视图中。这工作正常,但有20个滚动视图的全屏子视图导致一些滞后。我只在那里一次放置5个就解决了这个问题。当前视图,以及前两个和前两个。
现在的问题是,任何不是滚动视图子视图的Web视图都无法根据设备方向正确绘制。 webview的框架打印正确,但实际内容是以纵向模式绘制的。因此,内容右侧基本上有一条空白区域。
如何在不重新加载页面的情况下重新呈现内容?
以下是相关代码:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation{
[self resizeSubViews];
}
- (void) setupWebViews{
if(_webViews == nil)
_webViews = [[NSMutableArray alloc] init];
// This is where the navigation would come into play as far as loading up available web views
[_webViews removeAllObjects];
// for loop here to create web views
for (int i = 0; i < 20; i++) {
//CDCWebViewController *webView = [[[MyInternalWebView alloc] initWithFrame:_webViewWrapper.frame] retain];
MyInternalWebView *page = [[[MyInternalWebView alloc] init] retain];
[page loadRequest:[NSURLRequest requestWithURL:_url]];
[page setCdcIWVdelegate:self];
[page setTestIndex:i];
[page setPageIndex:i];
[_webViews addObject:page];
[self loadScrollViewWithPage:i];
}
[self clearUnusedWebViews:_pageControl.currentPage];
}
- (void) setupWebViewWrapper{
// a page is the width of the scroll view
_webViewWrapper.pagingEnabled = YES;
_pageControl = [[[UIPageControl alloc] init] retain];
_webViewWrapper.showsHorizontalScrollIndicator = NO;
_webViewWrapper.showsVerticalScrollIndicator = NO;
_webViewWrapper.scrollsToTop = NO;
_webViewWrapper.delegate = self;
_pageControl.numberOfPages = [_webViews count];
_pageControl.currentPage = 0;
}
- (void) resizeSubViews{
// The frame is set in IB
_webViewWrapper.contentSize = CGSizeMake(_webViewWrapper.frame.size.width * [_webViews count], _webViewWrapper.frame.size.height);
// Move the content offset.
_webViewWrapper.contentOffset = CGPointMake(_webViewWrapper.frame.size.width * _pageControl.currentPage, _webViewWrapper.contentOffset.y);
for (MyInternalWebView *subview in _webViewWrapper.subviews) {
// Reset the frame height and width here?
CGRect frame = _webViewWrapper.frame;
frame.origin.x = frame.size.width * subview.pageIndex;
frame.origin.y = 0;
[subview setFrame:frame];
}
}
//*****************************************************
//*
//* ScrollView Functions
//*
//*****************************************************
- (void)loadScrollViewWithPage:(int)page {
// Make sure we're not out of bounds
if (page < 0) return;
if (page >= [_webViews count]) return;
MyInternalWebView *webView = [_webViews objectAtIndex:page];
// Add the preloaded webview to the scrollview if it's not there already
if (nil == [webView superview]) {
CGRect frame = _webViewWrapper.frame;
//NSLog(@"width = %f", frame.size.width);
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
//NSLog(@"setting frame for page %d %@", page, NSStringFromCGRect(frame));
[webView setFrame:frame];
[_webViewWrapper addSubview:[_webViews objectAtIndex:page]];
// Now that the new one is loaded, clear what doesn't need to be here
[self clearUnusedWebViews:page];
}
}
- (void) clearUnusedWebViews: (NSInteger) page{
for (int i = 0; i < [_webViews count]; i++) {
if ((page - i) <= 2 && i - page <= 2) {
continue;
}
[[_webViews objectAtIndex:i] removeFromSuperview];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = _webViewWrapper.frame.size.width;
NSInteger page = floor((_webViewWrapper.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
_pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 2];
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
[self loadScrollViewWithPage:page + 2];
}
答案 0 :(得分:0)
重载
重新加载当前页面。
- (void)reload
状况
适用于iOS 2.0及更高版本。
另见
@property request @property loading – loadRequest: – stopLoading
声明
UIWebView.h
答案 1 :(得分:0)
在resizeSubViews中,从_webViews数组更改Web视图的帧,而不是滚动视图子视图。
附录:
如果页面!= _pageControl.currentPage,则检查scrollViewDidScroll,然后加载scrollViewsWithPage。
方向更改可能会触发scrollViewDidScroll。
在clearUnusedWebViews中使用_pageControl.currentPage而不是页面变量。当你在第5个视图并且你正在加载第7个时,你将删除第4个和第3个,甚至我说你的意图是在这个时候滚动视图中有3,4,5,6,7。