gpt4 book ai didi

ios uitableview 在滚动时淡化底部单元格和顶部单元格

转载 作者:可可西里 更新时间:2023-11-01 04:10:53 24 4
gpt4 key购买 nike

我在 uitableview 的单元格滚动出 View 时淡出,或者在它们滚动到 View 中时淡入。我面临的问题是,如果我滚动得非常快,有时完全可见的单元格仍然会变暗。下面是我的代码:

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

知道为什么当我快速滚动时, View 中的单元格有时会保持暗淡吗?

最佳答案

问题是滚动事件不会总是足够频繁地发生,以至于当单元格从顶部/底部滚动到中间时,滚动到 View 中的单元格将从褪色的 alpha 变为 1.0 的 alpha。但是,当您滚动得非常快时,scrollViewDidScroll 的调用频率不足以确保出现这种情况。你的循环显然试图重置 alpha 正在更新错误 View 的 alpha (contentView 而不是单元格本身)。

您可以通过替换现有的 for 循环来解决此问题:

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

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

或者,或者,从那里消除该循环,然后替换设置顶部和底部单元格的 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 应用渐变 mask 并退出 scrollViewDidScroll 方法。这里唯一的技巧是你不能将渐变蒙版应用到 TableView 本身(否则渐变将与 TableView 一起滚动),而是将 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;
}

诚然,这是一个略有不同的效果,但有时它是可取的。这仅取决于您的拍摄目的。

关于ios uitableview 在滚动时淡化底部单元格和顶部单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22726103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com