Iphone 是否有一种方法可以设置UITableViewCell的动画,使行短暂闪烁到选定状态,然后在选定时返回白色?
我只想在用户点击单元格时,在将其动画设置回未选中状态之前,使用UIColor或背景图像短暂高亮显示该单元格。我正在用星号标记单元格,以表明它已被选中,但希望整个单元格短暂高亮显示以显示所选内容。在didSelectRowAtIndexPath中添加此行-Iphone 是否有一种方法可以设置UITableViewCell的动画,使行短暂闪烁到选定状态,然后在选定时返回白色?,iphone,ios,animation,uitableview,Iphone,Ios,Animation,Uitableview,我只想在用户点击单元格时,在将其动画设置回未选中状态之前,使用UIColor或背景图像短暂高亮显示该单元格。我正在用星号标记单元格,以表明它已被选中,但希望整个单元格短暂高亮显示以显示所选内容。在didSelectRowAtIndexPath中添加此行- [tableView deselectRowAtIndexPath:indexPath animated:YES]; 如果您想更多地控制突出显示的外观,可以使用单元格标签的“突出显示”属性并设置selectedBackgroundView。选
[tableView deselectRowAtIndexPath:indexPath animated:YES];
如果您想更多地控制突出显示的外观,可以使用单元格标签的“突出显示”属性并设置selectedBackgroundView。选择后,两者都将短暂闪烁打开和关闭
// text color
[[cell textLabel] setTextColor:[UIColor blackColor]];
[[cell textLabel] setHighlightedTextColor:[UIColor whiteColor]];
// background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-background.png"]];
[cell setBackgroundView:backgroundView];
[backgroundView release];
UIImageView *selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-background-highlighted.png"]];
[cell setSelectedBackgroundView:selectedBackgroundView];
[selectedBackgroundView release];
我认为,如果您只想根据选择设置一行动画,那么提供的答案是可靠的 我正在寻找一种方法来同时处理多行,而不需要响应触摸选择和取消选择行。我做了以下工作: 在我的UITableViewController中创建了一个名为“callout单元格”的方法: 请注意,持续时间和延迟不会更改我选择的动画,因为我在setHighlighted上使用内置动画 当我想从UITableViewController启动高亮显示/取消高亮显示动画时,我可以同时高亮显示第0部分的第3行和第4行,如下所示:
[self calloutCells:[NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:3 inSection:0],
[NSIndexPath indexPathForRow:4 inSection:0], nil]];
希望这对其他人有帮助 swift3版本:
func calloutCell(indexPath: IndexPath) {
UIView.animate(withDuration: 0,
delay: 0,
options: .allowUserInteraction,
animations: {
let cell = self.tableView.cellForRow(at: indexPath)
cell?.setHighlighted(true, animated: true)
}) { (f: Bool) in
let cell = self.tableView.cellForRow(at: indexPath)
cell?.setHighlighted(false, animated: true)
}
}
单元格内:
self.selectionStyle = .blue
let uiview = UIView(frame: CGRect(x: 0, y: 0, width: 209, height: 251))
uiview.backgroundColor = UIColor.red
self.selectedBackgroundView = uiview
就这么简单!谢谢,你知道控制动画持续时间的方法吗?我认为用这种方法是不可能的。您可以使用单元格的自定义动画来完成此操作。如果您想控制动画,因为它需要的时间太长,您可能会喜欢当您将动画设置为false/NOVery simple(假/新)非常简单、非常优雅的解决方案时,动画闪烁的速度有多快。这非常有效,并且具有允许编程闪烁的附加优势(
didselectrowatinexpath
仅在实际点击的行上调用)。
self.selectionStyle = .blue
let uiview = UIView(frame: CGRect(x: 0, y: 0, width: 209, height: 251))
uiview.backgroundColor = UIColor.red
self.selectedBackgroundView = uiview