当它们从视图中滚动出时,我正在淡出uitableview的单元格,或者当它们向视图中滚动时,它们在淡出。我面临的问题是,如果我快速滚动,则有时完全可见的单元格仍会变暗。这是我的代码如下:
- (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;
}
}
}
关于为什么当我快速快速滚动查看时,有时看到的单元格仍然变暗的想法?
问题在于,滚动事件不会总是经常发生,以至于滚动到视图中的单元格从顶部/底部滚动到中间时,将从淡入的alpha变为1.0的alpha。但是,当您真正快速滚动时,scrollViewDidScroll
调用频率不够高,无法确保确实如此。而且您显然要重置的循环alpha
正在更新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;
}
}
顺便说一句,达到类似效果的另一种方法是将渐变蒙版应用于整个表视图并退出该scrollViewDidScroll
方法。这里唯一的技巧是您不能将渐变蒙版本身应用于表格视图(否则渐变将随表格视图滚动),而是将表格视图放入某个容器中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;
}
公认这是稍有不同的效果,但有时是可取的。这仅取决于您要拍摄什么。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句