在 iCarousel 中添加独立的进度视图以指示下载

Posted

技术标签:

【中文标题】在 iCarousel 中添加独立的进度视图以指示下载【英文标题】:Adding independent Progress VIew for indicate download in iCarousel 【发布时间】:2013-10-22 16:38:19 【问题描述】:

下午好。我在我的应用程序 iCarousel 中使用。目前我无法进行进度查看每个项目轮播。问题是当我单击“下载按钮”时,进度视图首先添加了一个项目。他看起来很好并且工作,但也从另一个角度出现在另一个 2 项目中,它不应该出现在。之后,当我再次单击“下载按钮”时,进度视图开始相互混淆。请准确地告诉我我做错了什么以及我的性交方式?我是 Objective-c 的新手。

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

iCarousel:

    - (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。在 Method iCarousel 我分配 Progress View tag = 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];

        

【问题讨论】:

【参考方案1】:

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

【讨论】:

我已经尝试过这样做。我在 viewForItemAtIndex 中创建了一个进度视图,以及按钮和一个标签,为它们提供了标签。但是不好也不会结果,错误是一样的。如果点击第一个下载视图,上面会出现Progress View,甚至第三个,为什么我不明白。如果你能写出我必须显示或隐藏的条件,我不想要进度视图。 您需要在重用视图时隐藏进度视图,这是不合适的——除非您这样做,否则您会看到同样的问题。这是一个视图重用问题... 您的数据模型应该告诉您您正在下载指定索引的项目,是吗? && (index == [self.carousel currentItemIndex]) 的目的是什么?可能想删除那个...

以上是关于在 iCarousel 中添加独立的进度视图以指示下载的主要内容,如果未能解决你的问题,请参考以下文章

Flutter Spinkit 进度指示器

如何在 iOS 中将选定的背景视图添加到 iCarousel?

在 iOS 的 icarousel 中使用 AsyncImageView 的问题

如何为每个页面加载颤动在 Web 视图中实现进度指示器?

如何调整 iCarousel 类型线性视图的框架以移动到顶部

UITableviews 作为 iCarousel 的子视图添加了滑动问题