我希望UITableViewCell
(-background(很快以红色亮起并再次恢复到旧外观。这是在NSTimer
的生命周期内完成的,该不断产生调用动画的冲动。
现在我正在使用 UIView 的 animateWithDuration:delay:options:animations:completion:
方法首先将单元格背景颜色更改为红色,然后在完成部分再次将其变为灰色(单元格的背景为灰色(。但是由于某种原因,此解决方案无法顺利运行。
问题:考虑到核心动画,我如何最好地为(a(UITableVeiwCell
和(b(UIButton
制作这种动画?
感谢您的任何帮助!
animateWithDuration是核心动画的UIView包装器,但你可以尝试在较低的级别上做,看看是否有变化。要在 CALayer 上执行显式 CA 事务,请执行以下操作:
[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[CATransaction setCompletionBlock:^{ //Your completion block }];
view.layer.backgroundColor = [UIColor whiteColor].CGColor;
[CATransaction commit];
其中视图是有问题的控件。对于UIButton,这只是按钮本身,对于UITableViewCell,这取决于表格是否分组,或者您是否使用自定义UITableViewCell。分组的表单元格将添加背景视图属性。
我使用NSTimer
一遍又一遍地触发动画。不知何故,它产生了上述副作用。现在我正在使用自iOS4
以来可用的块动画,它的工作更加流畅。您必须组合(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse)
选项才能重复动画:
[UIView animateWithDuration:kDurationOfCellAnimation delay:0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^{
//this will be animated in and out
cell.backgroundColor = CELL_RECORDING_COLOR;
}
completion:^(BOOL finished){
cell.backgroundColor = TABLEVIEW_BACKGROUND_COLOR;
}];
我遇到的唯一副作用是当单元格离开屏幕时动画停止,而NSTimer
并非如此。