ios uitableview fade bottom cell and top cell as you scroll

Atul Bhatia

I am fading out cells of my uitableview as they scroll out of view, or fading them in as they scroll into view. The issue I am facing is that if I scroll really fast sometimes the cells that are completely visible remain dimmed. Here is my code below:

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

Any idea as to why when I scroll really fast the cells in view sometimes remain dimmed?

Rob

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ios uitableview fade bottom cell and top cell as you scroll

From Dev

iOS collectionView: how to scroll a cell to the top of the screen

From Dev

Static cell at the bottom of a UITableView

From Dev

Unwind Segue, scroll to bottom of UITableView after adding cell

From Dev

Unwind Segue, scroll to bottom of UITableView after adding cell

From Dev

UIView on top of cell in UITableView

From Dev

How to scroll to bottom of UITableView to see all cell contents without adjusting cell height?

From Dev

How to scroll to bottom of UITableView to see all cell contents without adjusting cell height?

From Dev

iOS - UITableView Cell not touchable

From Dev

Scroll UITableView to display selected cell

From Dev

Remove cell padding on top and bottom

From Dev

UITableView Scrolls to Top on Cell Refresh

From Dev

Move uitableview cell to the top with animation

From Dev

Move uitableview cell to the top with animation

From Dev

Fade out 'scroll to top' button when you're 100px off the bottom of the page

From Dev

Scroll to top on click of onclick cell

From Dev

iOS UITableView Scroll to bottom of section

From Dev

Scroll to bottom of UITableView on iOS 7?

From Dev

UITableView jumps to top showing SearchBar when "Hide Bottom Bar on Push" is true for segue from Cell

From Dev

iOS: Resize cell in UITableview with Autoresizing

From Dev

iOS: Scroll one cell at a time

From Dev

iOS - Is there a way to select a cell but not scroll to it?

From Dev

iOS: Scroll one cell at a time

From Dev

Can't scroll to bottom of a cell in the datagridview

From Dev

Can't scroll to bottom of a cell in the datagridview

From Dev

uitableview cell back ground colour issue on scroll

From Dev

UITableView Crash on scroll , add or delete cell

From Dev

UITableView scroll to not-existsing cell programmatically

From Dev

Scroll to selected cell in UITableView after selecting cell in UISearchDisplayController?

Related Related

  1. 1

    ios uitableview fade bottom cell and top cell as you scroll

  2. 2

    iOS collectionView: how to scroll a cell to the top of the screen

  3. 3

    Static cell at the bottom of a UITableView

  4. 4

    Unwind Segue, scroll to bottom of UITableView after adding cell

  5. 5

    Unwind Segue, scroll to bottom of UITableView after adding cell

  6. 6

    UIView on top of cell in UITableView

  7. 7

    How to scroll to bottom of UITableView to see all cell contents without adjusting cell height?

  8. 8

    How to scroll to bottom of UITableView to see all cell contents without adjusting cell height?

  9. 9

    iOS - UITableView Cell not touchable

  10. 10

    Scroll UITableView to display selected cell

  11. 11

    Remove cell padding on top and bottom

  12. 12

    UITableView Scrolls to Top on Cell Refresh

  13. 13

    Move uitableview cell to the top with animation

  14. 14

    Move uitableview cell to the top with animation

  15. 15

    Fade out 'scroll to top' button when you're 100px off the bottom of the page

  16. 16

    Scroll to top on click of onclick cell

  17. 17

    iOS UITableView Scroll to bottom of section

  18. 18

    Scroll to bottom of UITableView on iOS 7?

  19. 19

    UITableView jumps to top showing SearchBar when "Hide Bottom Bar on Push" is true for segue from Cell

  20. 20

    iOS: Resize cell in UITableview with Autoresizing

  21. 21

    iOS: Scroll one cell at a time

  22. 22

    iOS - Is there a way to select a cell but not scroll to it?

  23. 23

    iOS: Scroll one cell at a time

  24. 24

    Can't scroll to bottom of a cell in the datagridview

  25. 25

    Can't scroll to bottom of a cell in the datagridview

  26. 26

    uitableview cell back ground colour issue on scroll

  27. 27

    UITableView Crash on scroll , add or delete cell

  28. 28

    UITableView scroll to not-existsing cell programmatically

  29. 29

    Scroll to selected cell in UITableView after selecting cell in UISearchDisplayController?

HotTag

Archive