iphone - 如何在滚动时淡入到达顶部的表格 View 单元格的内容?

标签 iphone uitableview

大家好,我需要实现此功能。我需要在单元格的内容消失时淡入淡出,我的意思是当它到达表格 View 的顶部时。我不知道如何做到这一点。请帮忙我。我尝试使用 ScrollView 的方法,但不知道如何做到这一点。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 

最佳答案

对于任何想要它的人,这里有一些可以完全运行的现成的复制/粘贴代码:

最佳部分:此代码仅依赖于一个外部属性: self.tableView ,所以只需确保已设置完毕即可开始!

只需将此方法粘贴到 View Controller 中的某个位置即可:

#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  &&  [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;
        }
    }
}

别忘了添加<UIScrollViewDelegate>到您类(class)的“.h”文件!

您可能还想在 viewDidLoad 中的某个位置调用此方法像这样的方法:[self scrollViewDidScroll:self.tableView];这样底部单元格就会开始褪色(因为它通常被表格 View 边缘切断)。

提示:将表格 View 分隔符样式设置为无 ( self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; ),或者创建您自己的分隔符作为图像并将它们作为 subview 添加到单元格中,这样就不会出现分隔符消失的令人不快的效果不褪色。

在小于全屏的表格 View 中也能正常工作。

关于iphone - 如何在滚动时淡入到达顶部的表格 View 单元格的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5000354/

相关文章:

swift - 倒带时自动重新加载 TableViewController

ios - 我有一个分组的 UITableView,我需要让它看起来像一个普通的 UITableView

iphone - 使用与 arc4random 相同的方法的计数器会导致按钮保持静止。 (iOS)

ios - 在 ios 7 xcode 中单击 ScrollView 上方的按钮时如何滚动 ScrollView ?

swift - 将 UIToolBar 添加到 TableViewController (Swift)

ios - 从 Tableview 单元格中获取多于一个 TextField 的值?

iphone - 可以移动 UITableView 的索引吗?

ios - 如何通过swift检查iPhone是打开还是关闭状态?

iphone - 是drawRect : called on multiple threads when using a CATiledlayer?

iphone - 如何在Core Graphics/Quartz 2D中绘制圆角矩形?