0

下の図のようにiPhoneアプリを作成しようとしています。

ここに画像の説明を入力してください

これでは、いくつかのimageviewsを動的にuiscrollビューに追加しています。各imageviewには、UIButtonとUIProgressViewが含まれています。それぞれをクリックすると、異なるURLが表示され、各URLの読み込みが対応するUIProgressViewに表示されます。非同期メソッドを使用していますが、複数の進行状況ビューを同時にロードする必要があります。これが私が使用したコードです、

- (void)startDownload:(id)sender {

    UIButton *btn = (UIButton *)sender;
    int btnTag = btn.tag;
    selectedTag = btn.tag;

    for (int x=0; x < [urlArray count]; x++) {
        if (btnTag == x) {
            NSURL *url = [NSURL URLWithString:[urlArray objectAtIndex:x]];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];

            NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

            [NSURLConnection sendAsynchronousRequest:request
                                               queue:[NSOperationQueue mainQueue]
                                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                       NSLog(@"Expected length: %lld ",response.expectedContentLength);
                                   }];
            if (theConnection) {
                receivedData = [NSMutableData data];                    
            }else {
                NSLog(@"Connection failed");
            }            
        }
    }
}

- (void)makeMyProgressMove{

    UIImageView *image = (UIImageView *)[mainView viewWithTag:selectedTag];
    UIProgressView *currentProgress = (UIProgressView *)[image viewWithTag:selectedTag];
    NSLog(@"Prog tag: %d",currentProgress.tag);

    if(currentProgress)
    {
        float actualProgress =  (_receivedDataBytes / (float)_totalFileSize);
        currentProgress.progress =  actualProgress;

    } else {
        NSLog( @"couldn't get the progress view for the image with tag: %d", selectedTag );
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    _totalFileSize = response.expectedContentLength;
    receivedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    _receivedDataBytes += [data length];

    NSLog(@"Receiving data: %2f", _receivedDataBytes / (float)_totalFileSize);

    if ((_receivedDataBytes / (float)_totalFileSize) < 1) {
        [self performSelectorOnMainThread: @selector(makeMyProgressMove) withObject: NULL waitUntilDone:NO];
    }
    else if ((_receivedDataBytes / (float)_totalFileSize) == 1){
        [self performSelectorOnMainThread: @selector(makeMyProgressMove) withObject: NULL waitUntilDone:NO];
        _receivedDataBytes = 0;
        _totalFileSize = 0;
    }
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
}

しかし、それは機能していません。何か案が?

4

1 に答える 1

1

異なるURLを使用して各UIWebViewを割り当て、各UIActivityIndi​​catorのすべてのUIWebView.GiveタグにaddSubViewUIActivityIndi​​catorViewを割り当てます。

-(void)action  {

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
UIWebView *webView = [[UIWebView alloc]initWithFrame:frame];//give y value incremented frame.

UIActivityIndicatorView  *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
av.frame=CGRectMake(145, 160, 25, 25);
av.center = webView.center;
av.tag  = INDICATOR_TAG;
[webView addSubview:av];
webView.delegate = self;
[av startAnimating];

//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

 UIActivityIndicatorView *indicator = [webView viewWithTag:INDICATOR_TAG];
indicator.hidden = YES;

}

このaction{}メソッドを呼び出して、複数回表示します。

編集

UIWebViewは、通常モードでは進行状況情報を提供しません。最初に、NSURLConnectionを使用して非同期でデータをフェッチする必要があります。NSURLConnectionデリゲートメソッドconnection:didReceiveResponseの場合、expectedContentLengthから取得した数値を取得し、それを最大値として使用します。次に、デリゲートメソッドconnection:didReceiveData内で、NSDataインスタンスのlengthプロパティを使用して、現在の距離を通知します。したがって、進行状況の割合は、0から1の間に正規化されたlength/maxLengthになります。

最後に、URLの代わりにデータを使用してWebビューを初期化します(connection:didFinishLoadingデリゲートメソッドで)。

参照: UIWebViewのロード中にUIProgressViewを使用する方法は?

于 2012-03-21T13:02:52.087 に答える