在UIWebView加载时设置加载gif

时间:2011-08-22 08:38:23

标签: iphone objective-c ipad uiwebview

我正在寻找一种简单的方法来显示自定义加载.gif或其他图像,只要UIWebView仍在加载页面。

我知道我必须在

中这样做
– webViewDidStartLoad:(UIWebView *)webView

但是如何加载图片?

1 个答案:

答案 0 :(得分:2)

试试这个:

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

    }

并在webViewDidFinishLoad:方法中将其从superview

中删除
- (void)webViewDidFinishLoad:(UIWebView *)webView {

    UIView * removeAminatingImages = [self.webView viewWithTag:1];
    [removeAminatingImages removeFromSuperview];
}