我正在淡入淡出當前視圖中的可用視圖的單元格,或者在他們滾動查看時將其淡入淡出狀態。我面臨的問題是,如果我滾動真的很快,有時完全可見的單元格保持暗淡。這裏是我的代碼如下:ios 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;
}
}
}
任何想法,爲什麼當我滾動真快鑑於細胞有時仍然黯淡?
這似乎不會導致問題了......但我很困惑如何for循環在我的代碼不處理這個。如果你看,我在上面的代碼中有這個for循環: 'for(UITableViewCell * cells in visibleCells){ cell.contentView.alpha = 1.0; }' –
@AtulBhatia哦,我不確定你在那裏試圖做什麼。這是因爲你正在設置'cell.contentView.alpha'而不是'cell.alpha'。當你昏暗時,你正在改變'cell.alpha',所以當你不昏暗時,你必須這樣做。 (如果你使用披露指標,單元格陰影等,你一定要使用'cell.alpha')。我已經更新了我的答案。 – Rob
啊,是的,我的錯誤是愚蠢的。謝謝你的協助! –