验证后在willSelectRowAtIndexPath中动画

Bas*_*ian 5 animation uitableview ios

我有一个表视图委托,它检查是否可以选择特定的单元格.如果不是,则中止选择.为了给用户提供视觉反馈,我想将这个细胞的标签染成红色,并在短时间后将其染成黑色:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (true) {                    // Some simplification
        MyTableViewCell cell = ... // The correct cell is loaded here
        [UIView animateWithDuration:0.5 animations:^{
            cellToSelect.labelAmount.textColor = [UIColor redColor];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:1.0 animations:^{
                cellToSelect.labelAmount.textColor = [UIColor blackColor];
            }];
        }];
        return nil;
    }
    return indexPath;
}
Run Code Online (Sandbox Code Playgroud)

动画未执行.相反,只是(视觉上)取消选择单元格.

编辑:我刚刚尝试了提出的解决方案,似乎都没有工作.所以我进一步挖掘并发现我可以做动画但是无法更改单元格内任何标签的textColor:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell cell = ...
    cell.labelAmount.textColor = [UIColor redColor];
    // Now, although the property was set (as I can see in the debugger) 
    // the label is still drawn with standard black text. 
}
Run Code Online (Sandbox Code Playgroud)

此外,通过丰富多彩的属性字符串设置颜色不起作用.

另一方面,highlightedTextColor相应地呈现变化.这样可行.

Mac*_*och 1

根据苹果文档,你不能为你想要的一切设置动画:

来自苹果:

UIView 类的以下属性是可动画的:

  • @属性框架
  • @属性边界
  • @物业中心
  • @属性变换
  • @属性阿尔法
  • @property背景颜色
  • @属性内容拉伸

现在这是一个让你想要的动画的技巧:

例如,将 alpha 设置为 1.0 - 将导致视图没有视觉变化,但会启动动画

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (true) {                    // Some simplification
        MyTableViewCell cell = ... // The correct cell is loaded here
        [UIView animateWithDuration:0.5 animations:^{

            //here the trick set alpha to 1
            self.view.alpha = 1;

            cellToSelect.labelAmount.textColor = [UIColor redColor];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:1.0 animations:^{
                cellToSelect.labelAmount.textColor = [UIColor blackColor];
            }];
        }];
        return nil;
    }
    return indexPath;
}
Run Code Online (Sandbox Code Playgroud)