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

Objective c XCode自定义活动指示器-加载UIWebView后隐藏(webViewDidFinishLoad),objective-c,ios,xcode,uiimageview,activity-indicator,Objective C,Ios,Xcode,Uiimageview,Activity Indicator,我正在使用这里找到的代码在当前版本的xCode中创建一个自定义的activityIndicator。此代码使用数组中的一系列.png图像,然后以设置的间隔显示它们,以创建动画加载图像,然后您可以随时将其放置在屏幕上(假设您可以以某种方式删除) 在我的主ViewController.m文件中,我的viewDidLoad部分中有以下代码: /* --- START CUSTOM ACTIVITY INDICATOR */ //Create the first status image and

我正在使用这里找到的代码在当前版本的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
时清除gif。有人帮助吗?

在头文件中将activityImageView声明为属性,并在
viewDidLoad:
方法中配置它

您应该在UIWebViewDelegate方法中添加activityImageView

- (void)webViewDidStartLoad:(UIWebView *)webView
然后在UIWebViewDelegate方法中
停止设置动画
从SuperView移除

- (void)webViewDidFinishLoad:(UIWebView *)webView
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
你应该阅读和阅读

更新

在标题中:

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

在头文件中将activityImageView声明为属性,并在
viewDidLoad:
方法中配置它

您应该在UIWebViewDelegate方法中添加activityImageView

- (void)webViewDidStartLoad:(UIWebView *)webView
然后在UIWebViewDelegate方法中
停止设置动画
从SuperView移除

- (void)webViewDidFinishLoad:(UIWebView *)webView
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
你应该阅读和阅读

更新

在标题中:

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

好的,您无法访问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
}

好的,您无法访问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
}

你应该这样做:

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

}

你应该这样做:

- (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?或者更好,改变可见性?或者我不明白是什么阻止你在你提到的两个函数中调用它,也许你可以澄清你遇到的问题?你能告诉我怎么做吗?:)比如什么代码,放在哪里?我很快就明白了。问题(我想我有,但老实说我真的不知道)是
activityImageView
无法在
viewDidLoad
部分之外访问…?啊,好吧,我知道你需要做什么,我将在回答部分发布。动画完成后,您是否尝试从当前视图中删除activityImageView?或者更好,改变可见性?或者我不明白是什么阻止你在你提到的两个函数中调用它,也许你可以澄清你遇到的问题?你能告诉我怎么做吗?:)比如什么代码,放在哪里?我很快就明白了。问题(我想我有,但老实说我不知道)是
activityImageView
viewDidLoad
部分之外是无法访问的…?啊,好吧,我知道你需要做什么,我会在答案部分发布。你能给我一些示例代码吗?:)比如我如何初始化它(在你的第一段)你的应用程序的结构是什么?你的网络视图在哪里?您的主视图是webView,还是webView是主视图的一部分?在我的原始代码中,我启动动画,然后使用
[activityImageView startAnimating]将其填充到视图中
[self.view addSubview:activityImageView]。当我希望它消失时,如何删除它?当您完成加载后,您希望执行[self.activityImageView removeFromSuperview]以删除活动指示器。您能给我一些示例代码吗?:)比如我如何初始化它(在你的第一段)你的应用程序的结构是什么?你的网络视图在哪里?你的主要观点是什么