2012-12-15 124 views
0

我正在阅读器应用程序。当你阅读一本杂志时,它将显示前五页并逐一下载其余页面。有一个滚动视图来查看页面的缩略图。在开始时,如果页面需要下载,相应的缩略图视图的alpha值设置为0.5(缩略图图像在文件中,无需下载)。当下载页面时,我会将缩略图视图的值更新为1.0。我使用一个操作来下载页面,当下载一个时,我使用委托设置缩略图视图的alpha。 但是,当我更新缩略图视图的alpha值时,它仍然与开头一样。似乎alpha不起作用。我不知道我的代码有什么问题吗?一些片段如下:为什么我改变UIImageView的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++; 
    } 
} 
+0

没有足够的代码发布。如果我不得不猜测,你要么在IB中设置标签值错误,要么没有链接某个对象(可能是容器?)。 – Anton

+0

@Anton我将添加代码以创建thumbScrollView.updated.thanks以供查看。 – chancyWu

+0

目前看起来确定...在下载操作开始时添加此代码:finishedAtIndex:,它在运行时输出了什么内容? NSLog(@“%@%@%i%f”,thumbScrollView,[thumbScrollView viewWithTag:THUMBVIEW_OFFSET + index],index,[thumbScrollView viewWithTag:THUMBVIEW_OFFSET + index] .alpha); – Anton

回答

0

你使用操作队列下载图像 - >这样,你完成回调可能到达不在主线程,但你正试图更新你的UI无论如何 - 尝试包装你的用户界面交互到主线程dispatch_async:

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

感谢@Inafziger提供的线索。

相关问题