The issue is that the scroll event will not always happen frequently enough that a cell that scrolls into view will go from the faded alpha to an alpha of 1.0 as it scrolls from the top/bottom to the middle. But when you scroll really quickly, the scrollViewDidScroll
isn't called frequently enough to ensure that this is the case. And your loop that apparently is attempting to reset the alpha
is updating the alpha
of the wrong view (contentView
rather than the cell itself).
You can remedy this by replacing your existing for
loop:
for (UITableViewCell *cell in visibleCells) {
cell.contentView.alpha = 1.0;
}
With
for (UITableViewCell *cell in visibleCells) {
cell.alpha = 1.0;
}
Or, alternatively, eliminate that loop from there and then replace the lines that set the alpha of the top and bottom cells:
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;
}
}
By the way, another way to achieve a similar effect is to apply a gradient mask to the whole tableview and retire the scrollViewDidScroll
method. The only trick here is that you cannot apply the gradient mask to the table view, itself (or else the gradient will scroll with the table view), but rather put the tableview inside some container UIView
, and then apply the mask to that:
- (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;
}
This is admittedly a slightly different effect, but sometimes it's desirable. It just depends upon what you're shooting for.