Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>For anyone who wants it, here's some ready-to-go copy/paste-able code that is fully working:</strong></p> <p><em>Best Part: This code is only reliant on ONE outside property:</em> <code>self.tableView</code><em>, so just make sure that's set up and you're good to go!</em></p> <p>Just stick this method in your view controller somewhere:</p> <pre><code>#pragma mark - Scroll View Delegate Methods - (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 &amp;&amp; [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 = 2.5; /* 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.contentView.alpha = topCellOpacity; } if (bottomCell) { bottomCell.contentView.alpha = bottomCellOpacity; } } } </code></pre> <p>Don't forget to add <code>&lt;UIScrollViewDelegate&gt;</code> to your class's '.h' file!</p> <p>You may also want to put a call to this method somewhere in your <code>viewDidLoad</code> method like this: <code>[self scrollViewDidScroll:self.tableView];</code> so that the bottom cell will start out faded (since it's normally cut off by the table view edge).</p> <p>TIP: Set your table view separator style to none (<code>self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;</code>), or create your own separators as images and add them as a subview to your cells, so that you don't get the unpleasant effect of the separators disappearing without fading.</p> <p><em>Works just as well with smaller than full-screen table views.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload