2012-07-10 38 views
0

我使用此處找到的代碼http://blog.blackwhale.at/?p=336在當前版本的xCode中創建自定義activityIndicator。此代碼在數組中使用一系列.png圖像,然後按照設定的時間間隔顯示它們,以創建一個動畫加載圖像,然後您可以隨時放置在屏幕上(並假設您可以以某種方式將其刪除)。XCode自定義活動指標 - 在UIWebView加載後隱藏(webViewDidFinishLoad)

在我的主要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出現而webViewDidStartLoad,然後清除時webViewDidFinishLoad。有人幫忙?

+0

您是否嘗試過只是從當前視圖中刪除activityImageView動畫完成後?或者更好的是,改變能見度?或者,也許我不明白什麼阻止你在你提到的兩個函數中調用它,也許你可以澄清你遇到的問題? – 2012-07-10 20:39:01

+0

你能告訴我該怎麼做嗎? :)喜歡什麼代碼,並把它放在哪裏?我趕上很快。這個問題(我認爲我有,但老實說,我不知道)是'activityImageView'不能在'viewDidLoad'部分之外訪問...? – adamdehaven 2012-07-10 20:41:43

+0

好的,我知道你需要做什麼,我會在答案部分發布。 – 2012-07-10 20:43:00

回答

1

好了,這樣就不能訪問activityImageView的原因是因爲它在聲明瞭viewDidLoad函數。把它作爲一個屬性添加到你的ViewController類,你應該能夠做你想做的。確保你從viewDidLoad中刪除了activityImageView的聲明,並在那裏初始化它。

而且,當是要顯示活動的指標你的WebView?它是否有獨立的視圖控制器,還是包含在主視圖控制器中?這聽起來像你的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 
} 
+0

你能給我一些示例代碼? :)像我將如何初始化它(在你的第一段) – adamdehaven 2012-07-10 20:50:30

+0

什麼是你的應用程序的結構?你的webView在哪裏?您的主視圖是webView還是主視圖的webView部分? – 2012-07-10 21:00:23

+0

在我的原始代碼中,我開始動畫,然後用'[activityImageView startAnimating];''和'[self.view addSubview:activityImageView];'將其填充到視圖中。當我想讓它消失時,我該如何刪除它? – adamdehaven 2012-07-10 21:01:44

0

聲明你activityImageView在你的頭文件的屬性,並在您viewDidLoad:方法來配置它

您應該添加在UIWebViewDelegate方法

- (void)webViewDidStartLoad:(UIWebView *)webView 

您activityImageView然後stopAnimatingremoveFromSuperview在UIWebViewDelegate方法

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

您應該閱讀UIImageView documentationUIWebViewDelegate protocol reference

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

如何在我的viewDidLoad方法中配置它?以及如何在UIWebViewDelegate方法中添加我的activityImageView?你能更新你的代碼嗎?我會是SOOO欣賞:) – adamdehaven 2012-07-10 20:52:33

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

}