当您滚动时,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; } } } 

任何想法,为什么当我滚动真正快速的细胞在视图中有时保持暗淡?

问题是滚动事件不会总是频繁发生,滚动到视图中的单元格将从淡入淡出的alpha变为1.0的alpha,因为它从顶部/底部滚动到中间。 但是,当你真的很快滚动, scrollViewDidScroll不是经常调用,以确保这种情况。 而你的循环,显然是试图重置alpha是更新错误的视图的alphacontentView而不是单元格本身)。

你可以通过replace你现有的for循环来弥补:

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

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

或者,也可以从那里消除该循环,然后replace设置顶部和底部单元格的alpha的行:

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

附:

 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; } 

这当然是一个稍微不同的效果,但有时这是可取的。 这取决于你所拍摄的东西。