我有一个表视图委托,它检查是否可以选择特定的单元格。如果不是,则中止选择。为了给用户提供视觉反馈,我想将这个细胞的标签染成红色,经过一段时间后将其染成黑色:
- (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;
}
不会执行动画。相反,只是(视觉上)取消选择单元格。
编辑:我刚尝试了提出的解决方案,似乎都没有用。所以我进一步挖掘并发现我可以做动画但是无法更改单元格内任何标签的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.
}
此外,通过丰富多彩的属性字符串设置颜色不起作用。
另一方面,相应地呈现highlightedTextColor
的变化。所以这很有效。
答案 0 :(得分:3)
此属性 - textColor
- 不可动画。使用transitionWithView:duration:options:animations:completion:
[UIView transitionWithView:label duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
label.textColor = [UIColor redColor];
} completion:^(BOOL finished) {
[UIView transitionWithView:label duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
label.textColor = [UIColor blackColor];
} completion:nil];
}];
答案 1 :(得分:1)
符合Apple文档,您无法为所需内容制作动画:
来自苹果:
UIView类的以下属性是可动画的:
- @property frame
- @property bounds
- @property centre
- @property transform
- @property alpha
- @property backgroundColor
- @property contentStretch
现在这是一个动画制作动画的技巧:
例如,将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;
}