在 iCaroel 中添加独立的进度 VIew 以指示下载



下午好。我在我的应用程序中使用旋转木马。目前我无法使进度视图每个项目轮播。问题是当我单击"下载按钮"进度视图时,首先添加了一个项目。他看起来很好并且有效,但也出现在另一个视图中的另一个 2 个项目中,它不应该是。之后,当我再次单击" 下载按钮 " 进度视图开始相互混淆。请确切地告诉我我做错了什么以及我是如何的?我是objective-c的新手。

P.S 要下载数据,我使用 AFNetworking。

旋转木马:

    - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    NSDictionary *myDic =[magazinesInfo objectAtIndex:index];
    //Change image size
    UIImage *img = [UIImage imageWithImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@_img.png",docDir,[myDic objectForKey:@"title"]]] scaledToSize:CGSizeMake(370,513)];
    UIImageView *faceImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,768,1004)];
    UIImage *dwImage = [UIImage imageNamed:@"button.png"];
    UIImage *readImage = [UIImage imageNamed:@"read_button.png"];
    UIImage *deleteImage = [UIImage imageNamed:@"delete_button.png"];
    UIImage *cancelImage = [UIImage imageNamed:@"cancelButton.png"];
    if(view ==nil)
    {
        UILabel *nomer = [[UILabel alloc] initWithFrame:CGRectMake(345, 85+MY_OFFSET, 75, 29)];
        UILabel *nameMag = [[UILabel alloc] initWithFrame:CGRectMake(55, 720+MY_OFFSET, 658, 80)];
        UILabel *dateMag = [[UILabel alloc] initWithFrame:CGRectMake(55, 821+MY_OFFSET, 658, 23)];
        UIButton *downloadButton = [[UIButton alloc] initWithFrame:CGRectMake(321, 890+MY_OFFSET, 128, 37)];
        UIButton *readButton = [[UIButton alloc] initWithFrame:CGRectMake(246, 890+MY_OFFSET, 128, 37)];
        UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(385, 890+MY_OFFSET, 128, 37)];
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0+MY_OFFSET, 128, 37)];

        view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 1004)];
        view = faceImage;
        faceImage.image = nil;
        ((UIImageView *)view).image = nil;
        view.contentMode = UIViewContentModeCenter;

        //Magazine number
        nomer.backgroundColor = [UIColor clearColor];
        nomer.textAlignment = NSTextAlignmentCenter;
        [nomer setFont:[UIFont fontWithName:@"OpenSans-Light" size:36.0f]];
        nomer.textColor = [UIColor whiteColor];
        nomer.tag = 1;
        //Magazine name
        nameMag.backgroundColor = [UIColor clearColor];
        nameMag.textAlignment = NSTextAlignmentCenter;
        [nameMag setFont:[UIFont fontWithName:@"OpenSans-Light" size:30.0f]];
        nameMag.numberOfLines=2 ;
        nameMag.textColor = [UIColor blackColor];
        nameMag.tag = 3;
        //Date magazine
        dateMag.backgroundColor = [UIColor clearColor];
        dateMag.textAlignment = NSTextAlignmentCenter;
        [dateMag setFont:[UIFont fontWithName:@"OpenSans-Light" size:20.0f]];
        dateMag.textColor = [UIColor blackColor];
        dateMag.tag = 4;

        //Download button
        [downloadButton setBackgroundImage:dwImage forState:UIControlStateNormal];
        [downloadButton addTarget:self action:@selector(pressDownload:) forControlEvents:UIControlEventTouchUpInside];
        downloadButton.tag = 5;
        downloadButton.hidden = YES;
        //Read button
        [readButton setBackgroundImage:readImage forState:UIControlStateNormal];
        [readButton addTarget:self action:@selector(readMag:) forControlEvents:UIControlEventTouchUpInside];
        readButton.hidden=YES;
        readButton.tag = 8;
        //Delete button
        [deleteButton setBackgroundImage:deleteImage forState:UIControlStateNormal];
        [deleteButton addTarget:self action:@selector(deleteMag:) forControlEvents:UIControlEventTouchUpInside];
        deleteButton.hidden=YES;
        deleteButton.tag = 9;
        [cancelButton setBackgroundImage:cancelImage forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(deleteMag:) forControlEvents:UIControlEventTouchUpInside];
        cancelButton.hidden=NO;
        cancelButton.tag = 10;

        //Add label to view
        [view addSubview:nomer];
        [view addSubview:nameMag];
        [view addSubview:dateMag];

        //Add button to view
        [view addSubview:downloadButton];
        [view addSubview:readButton];
        [view addSubview:deleteButton];
        [view addSubview:cancelButton];
    }
    else
    {
        //Set tag to image
        ((UIImageView *)faceImage).image = (UIImage*)[view viewWithTag:2];
        //Set tag to label
        [[[view subviews]objectAtIndex:0]viewWithTag:1];
        [[[view subviews]objectAtIndex:1]viewWithTag:3];
        [[[view subviews]objectAtIndex:2]viewWithTag:4];

        //Set tag to button
        [[[view subviews]objectAtIndex:3]viewWithTag:5];
        [[[view subviews]objectAtIndex:4]viewWithTag:8];
        [[[view subviews]objectAtIndex:5]viewWithTag:9];
        [[[view subviews]objectAtIndex:6]viewWithTag:10];
    }
    //Hide button download and show read,delete button
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@_mag.pdf",docDir,[myDic objectForKey:@"title"]]] == YES)
    {
        [[[view subviews] objectAtIndex:4] setHidden:NO];
        [[[view subviews] objectAtIndex:5] setHidden:NO];
        [[[view subviews] objectAtIndex:3] setHidden:YES];
    }
    else
    {
        [[[view subviews] objectAtIndex:4] setHidden:YES];
        [[[view subviews] objectAtIndex:5] setHidden:YES];
        [[[view subviews] objectAtIndex:3] setHidden:NO];
    }
    //Hide date and name of  magazine when view changed
    if (index != [self.carousel currentItemIndex]) {
        [[[view subviews]objectAtIndex:1]setHidden:YES];
        [[[view subviews]objectAtIndex:2]setHidden:YES];
    }
    else{
        [[[view subviews]objectAtIndex:1]setHidden:NO];
        [[[view subviews]objectAtIndex:2]setHidden:NO];
    }

    ((UIImageView *)view).image = img;
    UILabel *nomer = [[view subviews]objectAtIndex:0];
    nomer.text = [myDic objectForKey:@"title"];
    UILabel *nameMag = [[view subviews]objectAtIndex:1];
    nameMag.text = @"Жить интересно!” №5 Путешествия как стиль жизни";
    UILabel *dateMag = [[view subviews]objectAtIndex:2];
    dateMag.text = [myDic objectForKey:@"date"];

    return view;
}

下载按钮操作:

- (IBAction)pressDownload:(id)sender
{

    NSLog(@"download button was pressed");
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDictionary *myDic = [magazinesInfo objectAtIndex:curID];
    NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    NSString *pdfFilePath = [NSString stringWithFormat:@"%@/%@_mag.pdf.tmp",docDir,[myDic objectForKey:@"title"]];
    NSString *newPdfNamePath = [NSString stringWithFormat:@"%@/%@_mag.pdf",docDir,[myDic objectForKey:@"title"]];
    //Test for Progress bar
    UIButton *pressedButton = (UIButton *)sender;
    UIView *superViewOfPressedButton = pressedButton.superview;
    UIProgressView *downloadProgress  = [[UIProgressView alloc] initWithFrame:CGRectMake(300, 950, 127, 8)];
    UILabel *downloadPrecent = [[UILabel alloc]initWithFrame:CGRectMake(430, 950, 60, 20)];

    [superViewOfPressedButton addSubview:downloadProgress];
    [superViewOfPressedButton addSubview:downloadPrecent];

    [downloadProgress setHidden:NO];
    [downloadPrecent setHidden:NO];

    NSLog(@"%@",sender);

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[myDic objectForKey:@"magazine"]]];
    AFURLConnectionOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:pdfFilePath append:NO];
    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
    {
            downloadProgress.progress = (float)totalBytesRead / totalBytesExpectedToRead;
            downloadPrecent.text =[NSString stringWithFormat:@"%1.0f%@ ",((float)totalBytesRead / totalBytesExpectedToRead)*100,@"%"];
    }];
    [operation setCompletionBlock:^{
        [fileManager moveItemAtPath:pdfFilePath toPath:newPdfNamePath error:NULL];
        [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@_mag.pdf.tmp",docDir,[myDic objectForKey:@"title"]] error:NULL];
        [downloadProgress setHidden:YES];
        [downloadPrecent setHidden:YES];
        NSLog(@"downloadComplete!");
        [carousel reloadData];
    }];
    [operation start];
}

致韦恩:对不起,但我无法理解你的问题。我以下载数据的方法传递字典中的URL。在方法旋转木马中,我分配进度视图标签= 7,然后添加条件隐藏,如下所示:

if (([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@_mag.pdf.tmp",docDir,[myDic objectForKey:@"title"]]] == YES) && (index == [self.carousel currentItemIndex]))
        {
            [[[view viewWithTag:7] setHidden:NO];
            [[[view viewWithTag:7] setHidden:NO];
        }
        else
        {
            [[[view viewWithTag:7] setHidden:YES];
            [[[view viewWithTag:7] setHidden:YES];
        }

不要在 pressDownload: 中创建和添加进度视图。相反,请在创建视图时创建它,但将其设置为 hidden 。然后,当您需要它时,只需取消隐藏它。重用视图时,请确保根据是否在该索引处下载项目来设置适当的值。

最新更新