XCode自定义活动指示器 - 在UIWebView加载后隐藏(webViewDidFinishLoad)

时间:2012-07-10 20:30:17

标签: objective-c ios xcode uiimageview activity-indicator

我正在使用http://blog.blackwhale.at/?p=336中的代码在当前版本的xCode中创建自定义activityIndicator。此代码在数组中使用一系列.png图像,然后以设定的间隔显示它们以创建动画加载图像,然后您可以随时将其放置在屏幕上(假设您可以以某种方式将其删除)。

在我的主ViewController.m文件中,我在viewDidLoad部分中有以下代码:

/* --- START CUSTOM ACTIVITY INDICATOR */
    //Create the first status image and the indicator view
    UIImage *statusImage = [UIImage imageNamed:@"activity1.png"];
    UIImageView *activityImageView = [[UIImageView alloc] 
                                      initWithImage:statusImage];


    //Add more images which will be used for the animation
    activityImageView.animationImages = [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@"activity1.png"],
                                         [UIImage imageNamed:@"activity2.png"],
                                         [UIImage imageNamed:@"activity3.png"],
                                         [UIImage imageNamed:@"activity4.png"],
                                         [UIImage imageNamed:@"activity5.png"],
                                         [UIImage imageNamed:@"activity6.png"],
                                         [UIImage imageNamed:@"activity7.png"],
                                         [UIImage imageNamed:@"activity8.png"],
                                         [UIImage imageNamed:@"activity9.png"],
                                         [UIImage imageNamed:@"activity10.png"],
                                         [UIImage imageNamed:@"activity11.png"],
                                         [UIImage imageNamed:@"activity12.png"],
                                         nil];


    //Set the duration of the animation (play with it
    //until it looks nice for you)
    activityImageView.animationDuration = 0.6;


    //Position the activity image view somewhere in 
    //the middle of your current view
    activityImageView.frame = CGRectMake(
                                         self.view.frame.size.width/2
                                         -statusImage.size.width/2, 
                                         self.view.frame.size.height/1.2
                                         -statusImage.size.height/1.2, 
                                         statusImage.size.width, 
                                         statusImage.size.height);

    //Start the animation
    [activityImageView startAnimating];


    //Add your custom activity indicator to your current view
    [self.view addSubview:activityImageView];


   /*  --- END CUSTOM ACTIVITY INDICATOR */

我想清除/删除/隐藏activityImageView元素,因为我只希望它在应用首次启动时显示,直到UIWebView完成加载。

我想打电话给这个,并在webViewDidStartLoad时显示gif,然后在webViewDidFinishLoad时清除。有人帮忙?

3 个答案:

答案 0 :(得分:2)

你应该这样:

- (void)viewDidLoad {
    [super viewDidLoad];

    //Create the first status image and the indicator view
    UIImage *statusImage = [UIImage imageNamed:@"activity1.png"];
    UIImageView *activityImageView = [[UIImageView alloc] 
                                  initWithImage:statusImage];


    //Add more images which will be used for the animation
    activityImageView.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"activity1.png"],
                                     [UIImage imageNamed:@"activity2.png"],
                                     [UIImage imageNamed:@"activity3.png"],
                                     [UIImage imageNamed:@"activity4.png"],
                                     [UIImage imageNamed:@"activity5.png"],
                                     [UIImage imageNamed:@"activity6.png"],
                                     [UIImage imageNamed:@"activity7.png"],
                                     [UIImage imageNamed:@"activity8.png"],
                                     [UIImage imageNamed:@"activity9.png"],
                                     [UIImage imageNamed:@"activity10.png"],
                                     [UIImage imageNamed:@"activity11.png"],
                                     [UIImage imageNamed:@"activity12.png"],
                                     nil];


    //Set the duration of the animation (play with it
    //until it looks nice for you)
    activityImageView.animationDuration = 0.6;


    //Position the activity image view somewhere in 
    //the middle of your current view
    activityImageView.frame = CGRectMake(
                                     self.view.frame.size.width/2
                                     -statusImage.size.width/2, 
                                     self.view.frame.size.height/1.2
                                     -statusImage.size.height/1.2, 
                                     statusImage.size.width, 
                                     statusImage.size.height);

    //Start the animation
    [activityImageView startAnimating];


    //Add your custom activity indicator to your current view
    [self.view addSubview:activityImageView];

    self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0)
                                                  target:self
                                                selector:@selector(loading)
                                                userInfo:nil
                                                 repeats:YES];

}

- (void)loading {

    if (!self.webView.loading) {
        [activityImageView stopAnimating];
        activityImageView.hidden = 1;
    } else {
        [activityImageView startAnimating];
    }

}

答案 1 :(得分:1)

好的,所以你无法访问activityImageView的原因是因为它在你的viewDidLoad函数中声明了。将它作为属性添加到您的ViewController类,您应该能够做你想要的。确保从viewDidLoad中删除了activityImageView的声明,并在那里初始化它。

此外,您想在哪个网页视图中显示活动指标?它是否有单独的视图控制器或它是否包含在主视图控制器中?听起来你的activityImageView的范围是不正确的,虽然不知道你怎么设置它有点难以告诉你把它放在哪里。

在这两个地方使用活动指示器之后,您是否计划在应用程序运行时再次使用它,或者只有在重新加载应用程序时它才会显示?根据您的答案,您可能只想隐藏它,或从主视图中删除它。

UIImageView.hidden控制可见性。

编辑,代码按要求:

ViewController.h

@interface ViewController : UIViewController
{
    UIWebView *_webView;
}

@property(nonatomic, strong) UIWebView *webView;
@property(nonatomic, strong) UIImageView *activityImageView;

ViewController.m

@implementation ViewController
@synthesize activityImageView;
@synthesize webView = _webView;

-(void)viewDidLoad {

    //all your previous stuff with the change that you just alloc activityImageView instead of declare it
    activityImageView = [[UIImageView alloc] initWithImage:statusImage];
    //The above is the initialization, below is where your old code should go
}

答案 2 :(得分:0)

将您的activityImageView声明为头文件中的属性,并在viewDidLoad:方法中对其进行配置

您应该在UIWebViewDelegate方法

中添加您的activityImageView
- (void)webViewDidStartLoad:(UIWebView *)webView

然后在UIWebViewDelegate方法中使用stopAnimatingremoveFromSuperview

- (void)webViewDidFinishLoad:(UIWebView *)webView
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

您应该阅读UIImageView documentationUIWebViewDelegate protocol reference

更新

在标题中:

@property(nonatomic, retain) UIImageView *activityImageView;

在您的实施中

@synthesize activityImageView;

-(void)viewDidLoad{
  activityImageView = [[UIImageView alloc] 
                                      initWithImage:statusImage];


    //Add more images which will be used for the animation
    activityImageView.animationImages = [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@"activity1.png"],
                                         [UIImage imageNamed:@"activity2.png"],
                                         [UIImage imageNamed:@"activity3.png"],
                                         [UIImage imageNamed:@"activity4.png"],
                                         [UIImage imageNamed:@"activity5.png"],
                                         [UIImage imageNamed:@"activity6.png"],
                                         [UIImage imageNamed:@"activity7.png"],
                                         [UIImage imageNamed:@"activity8.png"],
                                         [UIImage imageNamed:@"activity9.png"],
                                         [UIImage imageNamed:@"activity10.png"],
                                         [UIImage imageNamed:@"activity11.png"],
                                         [UIImage imageNamed:@"activity12.png"],
                                         nil];


    //Set the duration of the animation (play with it
    //until it looks nice for you)
    activityImageView.animationDuration = 0.6;


    //Position the activity image view somewhere in 
    //the middle of your current view
    activityImageView.frame = CGRectMake(
                                         self.view.frame.size.width/2
                                         -statusImage.size.width/2, 
                                         self.view.frame.size.height/1.2
                                         -statusImage.size.height/1.2, 
                                         statusImage.size.width, 
                                         statusImage.size.height);
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
[self.activityImageView startAnimating];
[self.view addSubview:activityImageView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
[activityImageView stopAnimating];
[activityImageView removeFromSuperview];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[activityImageView stopAnimating];
[activityImageView removeFromSuperview];
}