为什么我改变UIImageView的alpha值但没有效果?

时间:2012-12-15 04:51:43

标签: ios uiview uiimageview

我正在开发一款读者应用。当您阅读一本杂志时,它将显示前五页并逐一下载其余页面。有一个滚动视图来查看页面的缩略图。开始时,如果页面需要下载,则相应的缩略图视图的alpha值设置为0.5(缩略图图像在文件中,无需下载)。下载页面时,我会将缩略图视图的值更新为1.0。我使用一个操作来下载页面,当下载一个时,我使用委托来设置缩略图视图的alpha。   但是当我更新缩略图视图的alpha值时,它仍然与开头相同。似乎alpha没有效果。我想知道我的代码有什么问题吗?一些片段如下:

在PageViewController.m

- (void)loadView
{
   [super loadView];
   //...
   [self createSlideUpViewIfNecessary];
   [self downloadPages];
}
- (void)createSlideUpViewIfNecessary {
    if (!slideUpView) {
        [self createThumbScrollViewIfNecessary];

        // create container view that will hold scroll view and label
        CGRect frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMaxY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(thumbScrollView.frame));
        slideUpView = [[UIView alloc] initWithFrame:frame];
        [slideUpView setBackgroundColor:[UIColor blackColor]];
        [slideUpView setOpaque:NO];
        [slideUpView setAlpha:0.75];
        [[self view] addSubview:slideUpView];

        // add subviews to container view
        [slideUpView addSubview:thumbScrollView];
    }
}

- (void)createThumbScrollViewIfNecessary {
    if (!thumbScrollView) {
        float scrollViewHeight = THUMB_HEIGHT + THUMB_V_PADDING;
        float scrollViewWidth  = CGRectGetWidth(self.view.bounds);
        thumbScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)];
        [thumbScrollView setCanCancelContentTouches:NO];
        [thumbScrollView setClipsToBounds:NO];

        // now place all the thumb views as subviews of the scroll view
        // and in the course of doing so calculate the content width
        float xPosition = THUMB_H_PADDING;

        for (int i = 0; i < magazine.pageNum ; i++) {
            Page *page = [magazine.pages objectAtIndex:i];
            NSString *name = page.pageName;

            NSString *mfjName =[name stringByReplacingOccurrencesOfString:@".mfj" withString:@"Small.mfj"];
            UIImage *thumbImage = nil;
            if([mfjName hasSuffix:@".mfj"])
                thumbImage = [Reader loadMfjFromSprOrCache:magazine MFJ:mfjName];

            ThumbImageView *thumbView;
            if (thumbImage) {// sometimes mfjname is 0 which means white page in normal and black thumbnail in thumbnail scrollview.
                if (!mThumbnailSizeUpdated) {
                    mThumbnailWidth = thumbImage.size.width;
                    mThumbnailHeight = thumbImage.size.height;
                    mThumbnailSizeUpdated = YES;
                }
                thumbView = [[ThumbImageView alloc] initWithImage:thumbImage];
            } else {
                CGRect thumbFrame;
                if (mThumbnailSizeUpdated) {
                    thumbFrame = CGRectMake(0, 0, mThumbnailWidth, mThumbnailHeight);
                } else {
                    mThumbnailWidth = 80;
                    mThumbnailHeight = 100;
                    thumbFrame = CGRectMake(0, 0, mThumbnailWidth, mThumbnailHeight);
                }
                thumbView = [[ThumbImageView alloc] initWithFrame:thumbFrame];
            }
            NSString *mfjPath= [[magazine getDownloadPath] stringByAppendingPathComponent:name];
            if (![magazine getFileInfo:name]&&![[NSFileManager defaultManager] fileExistsAtPath:mfjPath]) {
                thumbView.alpha = 0.5;
            }
            [thumbView setBackgroundColor:[UIColor blackColor]];
            [thumbView setTag:THUMBVIEW_OFFSET+i];
            [thumbView setDelegate:self];
            [thumbView setImageName:name];
            CGRect frame = [thumbView frame];
            frame.origin.y = THUMB_V_PADDING;
            frame.origin.x = xPosition;
            frame.size.width = frame.size.width+30;
            frame.size.height = frame.size.height+40;
            [thumbView setFrame:frame];
            [thumbScrollView addSubview:thumbView];
            UILabel *pageIndexLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPosition, frame.origin.y+frame.size.height-THUMB_LABEL_HEIGHT, frame.size.width, THUMB_LABEL_HEIGHT)];
            [pageIndexLabel setBackgroundColor:[UIColor clearColor]];
            [pageIndexLabel setText:[NSString stringWithFormat:@"%d",(i+1)]];
            [pageIndexLabel setTextColor:[UIColor whiteColor]];
            [pageIndexLabel setTextAlignment:UITextAlignmentCenter];
            [thumbScrollView addSubview:pageIndexLabel];

            xPosition += (frame.size.width + THUMB_H_PADDING);
        }
        thumbScrollView.showsHorizontalScrollIndicator = NO;
        [thumbScrollView setContentSize:CGSizeMake(xPosition, scrollViewHeight)];
    }
}
- (void)downloadPages
{
   DownloadOperation *op = [[DownloadOperation alloc] initWithMagazine:magazine];
   op.delegate = self;
   [[(AppDelegate *)[[UIApplication sharedApplication] delegate] sharedOperationQueue] addOperation:op];
}

- (void)downloadOperation:(DownloadOperation *)operation finishedAtIndex:(NSUInteger)index
{
   if (thumbScrollView){
      [thumbScrollView viewWithTag:THUMBVIEW_OFFSET+index].alpha = 1.0;
   }
}

在DownloadOperation.m中

- (void)main
{
   // ...
   NSUInteger index = 0;
   for (Page *page in mMagazine.pages)
   {
       if (/*page doesn't exist*/){
           // download the page;
           if ([delegate respondsToSelector:@selector(downloadOperation:finishedAtIndex:)]) {
                        [delegate downloadOperation:self finishedAtIndex:index];
            }
       }
       index++;
   }
}

1 个答案:

答案 0 :(得分:0)

您正在使用操作队列下载图像 - &gt;因此,您的完成回调可能不会到达主线程,但您仍尝试更新UI - 尝试将UI交互包装到主线程上的dispatch_async:

 dispatch_async(dispatch_get_main_queue), ^{
     if (thumbScrollView){
         [thumbScrollView viewWithTag:THUMBVIEW_OFFSET+index].alpha = 1.0;
     }
 });

感谢@Inafziger提供线索。