重绘UIWebView的内容

发布于 2024-11-08 14:54:21 字数 4275 浏览 0 评论 0原文

我有一堆带有静态 html 内容的 webview,我将它们作为页面放入滚动视图中。这工作得很好,但是滚动视图的 20 个全屏子视图会导致一些延迟。我通过一次只放 5 个来解决这个问题。当前视图、下两个视图和上一个视图。

现在的问题是,任何不是滚动视图子视图的 Web 视图都无法根据设备方向正确绘制。网络视图的框架打印正确,但实际内容是以纵向模式绘制的。因此,内容右侧基本上有一条空白区域。

如何在不重新加载页面的情况下重新渲染内容?

这是相关代码:

- (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];

}

I have a bunch of webviews with static html content that I'm putting in a scroll view as pages. That works fine, but having 20 something full screen subviews of the scroll view is causing some lag. I solved that by only placing 5 at a time in there. The current view, and the two next and two previous.

The problem now is that any web view that is not a subview of the scroll view is not drawing correctly based on the device orientation. The frame of the webview is printing out correct, but the actual content is drawing in portrait mode. So there is essentially a strip of blank space to the right of the content.

How do I go about re rendering the content without reloading the page?

Here's the relevant code:

- (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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左岸枫 2024-11-15 14:54:21

在 resizeSubViews 中,更改 _webViews 数组中的 Web 视图框架,而不是滚动视图子视图。

附录:

  • 如果 page != _pageControl.currentPage 则检查scrollViewDidScroll,然后才加载scrollViewsWithPage。

  • 方向改变可能会触发scrollViewDidScroll。

  • 在clearUnusedWebViews中使用_pageControl.currentPage而不是页面变量。当您处于第 5 个视图并加载第 7 个视图时,您正在删除第 4 个和第 3 个视图,即使我想说您此时的意图是在滚动视图中显示 3、4、5、6、7。

In resizeSubViews, change frames of web views from _webViews array, not scroll view subviews.

Addendum:

  • Put check in scrollViewDidScroll if page != _pageControl.currentPage and only then load scrollViewsWithPage.

  • Orientation change might and probably will trigger scrollViewDidScroll.

  • In clearUnusedWebViews use _pageControl.currentPage instead of page variable. When you're at 5th view and you're loading 7th, you're removing 4th and 3rd even I'd say your intention was to have 3,4,5,6,7 in scroll view at this time.

蝶…霜飞 2024-11-15 14:54:21

重新加载

重新加载当前页面。

- (void)重新加载

可用性

适用于 iOS 2.0 及更高版本。

另请参阅

 @property 请求
  @属性加载
– 加载请求:
– 停止加载

声明于

UIWebView.h

UIWebView类参考

reload

Reloads the current page.

- (void)reload

Availability

Available in iOS 2.0 and later.

See Also

  @property request
  @property loading
– loadRequest:
– stopLoading

Declared In

UIWebView.h

UIWebView Class Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文