Set loading gif in UIWebView while it's loadin

2019-06-13 18:54发布

I'm searching for an easy method to display a custom loading .gif or another image as long as a UIWebView is still loading the page.

I know I have to do it in

– webViewDidStartLoad:(UIWebView *)webView

But how do I load the image?

1条回答
2楼-- · 2019-06-13 19:27

try this :

    – webViewDidStartLoad:(UIWebView *)webView {

        UIImageView   *animatedImages;
        NSMutableArray *imageArray;

            // Array to hold jpg images
        imageArray = [[NSMutableArray alloc] initWithCapacity:IMAGE_COUNT];

            // Build array of images, cycling through image names
        for (int i = 0; i < IMAGE_COUNT; i++)
            [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"yourGifImage%d.jpg", i]]];

            // Animated images - centered on screen
        animatedImages = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,IMG_HEIGHT,IMG_WIDTH)];

           animatedImages.center = self.webView.center;

        animatedImages.animationImages = [NSArray arrayWithArray:imageArray];

            // One cycle through all the images takes 1 seconds
        animatedImages.animationDuration = 1.0;

            // Repeat forever
        animatedImages.animationRepeatCount = 0;

        animatedImages.tag = 1;

        [animatedImages startAnimating];

            // Add to webview
        [self.webView addSubview:animatedImages];

    }

and in webViewDidFinishLoad: method remove it from superview

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    UIView * removeAminatingImages = [self.webView viewWithTag:1];
    [removeAminatingImages removeFromSuperview];
}
查看更多
登录 后发表回答