XCode自定义活动指标 – 在UIWebView加载后隐藏(webViewDidFinishLoad)

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

在我的主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 */ 

我想CLEAR / DELETE /隐藏activityImageView元素,因为我只希望它显示应用程序第一次启动,直到UIWebView完成加载。

我想调用此方法,并在webViewDidStartLoad时显示gif,然后在webViewDidFinishLoadwebViewDidFinishLoad 。 有人帮忙?

好吧,所以你不能访问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 } 

你应该这样做:

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

在你的头文件中声明你的activityImageView作为属性,并在你的viewDidLoad:方法中configuration它

您应该在UIWebViewDelegate方法中添加您的activityImageView

 - (void)webViewDidStartLoad:(UIWebView *)webView 

然后在UIWebViewDelegate方法中stopAnimatingstopAnimating removeFromSuperview stopAnimating

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

您应该阅读UIImageView文档和UIWebViewDelegate协议参考

UPDATE

在你的标题中:

 @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]; }