0

我正在使用此处找到的代码http://blog.blackwhale.at/?p=336activityIndicator在当前版本的 xCode 中创建自定义。此代码使用.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完成加载。

我想调用它并让 gif 出现 whilewebViewDidStartLoad然后清除 when webViewDidFinishLoad。有人帮忙??

4

3 回答 3

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

}
于 2013-09-30T02:58:26.337 回答
1

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

此外,您要在哪里显示活动指示器的 web 视图?它有单独的视图控制器还是包含在您的主视图控制器中?听起来您的 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
}
于 2012-07-10T20:44:02.193 回答
0

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

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

- (void)webViewDidStartLoad:(UIWebView *)webView

然后在 UIWebViewDelegate 方法stopAnimatingremoveFromSuperview

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

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

更新

在您的标题中:

@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];
}
于 2012-07-10T20:43:03.133 回答