简体   繁体   English

使用accessoryView为UITableViewCell的backgroundColor设置动画

[英]Animate the backgroundColor of a UITableViewCell with an accessoryView

Currently, I'm working on a UITableViewController. 目前,我正在开发一个UITableViewController。 In the cells of its UITableView it presents real-time data from a web service. 在其UITableView的单元格中,它提供来自Web服务的实时数据。 When one of the underlying data items is updated (once every two minutes or so) I want the cell to "flash" briefly, so the user understands, that the data for that cell just has been updated. 当更新其中一个基础数据项(每两分钟左右一次)时,我希望单元格“闪烁”一下,以便用户理解该单元格的数据刚刚更新。

Up to now, I used this code: 到目前为止,我使用了这段代码:

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut animations:^
{
    cell.contentView.backgroundColor = flashColor;
} completion:^(BOOL finished)
{
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut animations:^
    {
        cell.contentView.backgroundColor = [UIColor clearColor];
    } completion: NULL];
}];

This worked great, until I wanted to provide a way for the user to "look inside" the data for a given cell and added Disclosure Indicators. 这很有效,直到我想为用户提供一种“查看”给定单元格数据并添加Disclosure Indicators的方法。 Since the content area is shrunk by the framework to make room for the Disclosure Indicator, now the flash only highlights the left 90% of the cell, but the background color of the Disclosure Indicator does not change. 由于框架缩小了内容区域以便为Disclosure Indicator腾出空间,现在flash只突出显示单元格的左侧90%,但Disclosure Indicator的背景颜色不会改变。

cell.accessoryView.backgroundColor = flashColor;

and

cell.backgroundView.backgroundColor = flashColor;

won't help to fix the animation. 无助于修复动画。

I've read about - (void) setHighlighted: (BOOL)highlighted animated: (BOOL)animated , but that won't revert the highlighted state immediately after the flash, unless I write a bulk of nasty error-prone code that keeps it from flying apart. 我已经读过- (void) setHighlighted: (BOOL)highlighted animated: (BOOL)animated ,但是在闪存之后不会立即恢复突出显示的状态,除非我写了大量令人讨厌的容易出错的代码来保存它从分开飞行。 Also, I won't have any control over the animation itself. 此外,我无法控制动画本身。

Is there a way to keep the old animation effect with an accessory view present, or do I have to start doing the flash using the highlight method? 有没有办法保持旧的动画效果与附件视图存在,或者我是否必须使用突出显示方法开始使用闪光灯?

Best Regards, Chris 最诚挚的问候,克里斯

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut animations:^
{
    [cell setHighlighted:YES animated:YES];
} completion:^(BOOL finished)
{
    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut animations:^
    {
        [cell setHighlighted:NO animated:YES];
    } completion: NULL];
}];

Note that the cell will ignore the time set in animateWithDuration parameter, and always uses the iOS-default of 0.2 seconds. 请注意,单元格将忽略animateWithDuration参数中设置的时间,并始终使用iOS默认值0.2秒。 So it is a good idea to set that parameter to 0.2. 因此,将该参数设置为0.2是个好主意。

The solution is simple, but of course the original animation could not be preserved (fast highlight and slow fade out). 解决方案很简单,但当然无法保留原始动画(快速突出显示和慢速淡出)。 On the other hand this is probably the more conservative and future-proof way to do it. 另一方面,这可能是更加保守和面向未来的方式。

Thanks to David Rönnqvist! 感谢DavidRönnqvist!

Chris 克里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM