2014-03-29 70 views
5

我正在淡入淡出當前視圖中的可用視圖的單元格,或者在他們滾動查看時將其淡入淡出狀態。我面臨的問題是,如果我滾動真的很快,有時完全可見的單元格保持暗淡。這裏是我的代碼如下:ios uitableview在您滾動時淡入淡出底部單元格和頂部單元格

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    // Fades out top and bottom cells in table view as they leave the screen 
    NSArray *visibleCells = [self.tableView visibleCells]; 

    if (visibleCells != nil && [visibleCells count] != 0) {  // Don't do anything for empty table view 

     /* Get top and bottom cells */ 
     UITableViewCell *topCell = [visibleCells objectAtIndex:0]; 
     UITableViewCell *bottomCell = [visibleCells lastObject]; 

     /* Make sure other cells stay opaque */ 
     // Avoids issues with skipped method calls during rapid scrolling 
     for (UITableViewCell *cell in visibleCells) { 
      cell.contentView.alpha = 1.0; 
     } 

     /* Set necessary constants */ 
     NSInteger cellHeight = topCell.frame.size.height - 1; // -1 To allow for typical separator line height 
     NSInteger tableViewTopPosition = self.tableView.frame.origin.y; 
     NSInteger tableViewBottomPosition = self.tableView.frame.origin.y + self.tableView.frame.size.height; 

     /* Get content offset to set opacity */ 
     CGRect topCellPositionInTableView = [self.tableView rectForRowAtIndexPath:[self.tableView indexPathForCell:topCell]]; 
     CGRect bottomCellPositionInTableView = [self.tableView rectForRowAtIndexPath:[self.tableView indexPathForCell:bottomCell]]; 
     CGFloat topCellPosition = [self.tableView convertRect:topCellPositionInTableView toView:[self.tableView superview]].origin.y; 
     CGFloat bottomCellPosition = ([self.tableView convertRect:bottomCellPositionInTableView toView:[self.tableView superview]].origin.y + cellHeight); 

     /* Set opacity based on amount of cell that is outside of view */ 
     CGFloat modifier = 1.2;  /* Increases the speed of fading (1.0 for fully transparent when the cell is entirely off the screen, 
           2.0 for fully transparent when the cell is half off the screen, etc) */ 
     CGFloat topCellOpacity = (1.0f - ((tableViewTopPosition - topCellPosition)/cellHeight) * modifier); 
     CGFloat bottomCellOpacity = (1.0f - ((bottomCellPosition - tableViewBottomPosition)/cellHeight) * modifier); 

     /* Set cell opacity */ 
     if (topCell) { 
      topCell.alpha = topCellOpacity; 
     } 
     if (bottomCell) { 
      bottomCell.alpha = bottomCellOpacity; 
     } 
    } 
} 

任何想法,爲什麼當我滾動真快鑑於細胞有時仍然黯淡?

回答

14

問題是,滾動事件不會總是頻繁發生,滾動到視圖中的單元格將從褪色的alpha值移動到1.0的alpha值,因爲它會從頂部/底部滾動到中間。但是當你真的很快滾動時,scrollViewDidScroll不會被頻繁調用以確保情況確實如此。而你的循環顯然是試圖重置alpha正在更新錯誤視圖(contentView而不是單元本身)的alpha

您可以通過替換現有的for循環解決這個問題:

for (UITableViewCell *cell in visibleCells) { 
    cell.contentView.alpha = 1.0; 
} 

隨着

for (UITableViewCell *cell in visibleCells) { 
    cell.alpha = 1.0; 
} 

,或者,從那裏消除環路然後更換設置的阿爾法線頂部和底部的單元格:

if (topCell) { 
    topCell.alpha = topCellOpacity; 
} 
if (bottomCell) { 
    bottomCell.alpha = bottomCellOpacity; 
} 

With:

for (UITableViewCell *cell in self.tableView.visibleCells) { 
    if (cell == topCell) { 
     cell.alpha = topCellOpacity; 
    } else if (cell == bottomCell) { 
     cell.alpha = bottomCellOpacity; 
    } else { 
     cell.alpha = 1.0; 
    } 
} 

順便說,另一種方式來實現類似的效果是將梯度掩模應用於整個的tableview和退休scrollViewDidScroll方法。這裏唯一的技巧是你不能將梯度蒙版應用到表格視圖本身(否則梯度將與表格視圖一起滾動),而是將tableview放在某個容器UIView內,然後將蒙版應用於:

- (void)viewDidAppear:(BOOL)animated 
{ 
    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = self.containerView.bounds; 
    gradient.colors = @[(id)[UIColor clearColor].CGColor, 
         (id)[UIColor whiteColor].CGColor, 
         (id)[UIColor whiteColor].CGColor, 
         (id)[UIColor clearColor].CGColor]; 
    gradient.locations = @[@0.0, @0.1, @0.9, @1.0]; 
    self.containerView.layer.mask = gradient; 
} 

這是毫無疑問的稍微不同的效果,但有時它是可取的。這取決於你所拍攝的內容。

+0

這似乎不會導致問題了......但我很困惑如何for循環在我的代碼不處理這個。如果你看,我在上面的代碼中有這個for循環: 'for(UITableViewCell * cells in visibleCells){ cell.contentView.alpha = 1.0; }' –

+1

@AtulBhatia哦,我不確定你在那裏試圖做什麼。這是因爲你正在設置'cell.contentView.alpha'而不是'cell.alpha'。當你昏暗時,你正在改變'cell.alpha',所以當你不昏暗時,你必須這樣做。 (如果你使用披露指標,單元格陰影等,你一定要使用'cell.alpha')。我已經更新了我的答案。 – Rob

+0

啊,是的,我的錯誤是愚蠢的。謝謝你的協助! –

相關問題