在uitableviewcell中的CABasicAnimation似乎不起作用

我试图用一个CABasicAnimation在tableview单元格内改变背景颜色的标签,它似乎并没有工作 – 单元格背景颜色保持稳定,没有animation。 此代码位于cellForRowAtIndexPath方法中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MainCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; NSString *name = @"Hello"; UIColor *color = [UIColor colorWithRed:0.0f/255.0f green:100.0f/255.0f blue:200.0f/255.0f alpha:1.0f]; // I'm setting the label as a strong property of the cell cell.label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, cell.contentView.frame.size.height)]; cell.label.text = name; cell.label.textColor = [UIColor whiteColor]; cell.label.backgroundColor = color; [cell.contentView addSubview:cell.label]; UIColor *endColor = [UIColor redColor]; CABasicAnimation *animation; animation=[CABasicAnimation animationWithKeyPath:@"backgroundColor"]; animation.duration=0.7; animation.repeatCount=HUGE_VALF; animation.autoreverses=YES; animation.fromValue=(id)color.CGColor; animation.toValue=(id)endColor.CGColor; [cell.label.layer addAnimation:animation forKey:@"pulses"]; return cell; } 

我想出了这个问题,但我不确定为什么会这样。 如果其他人可以添加到这个答案,请随时免费。

看起来好像设置单元格标签的背景颜色隐藏了图层的animation。 如果我注释掉标签的backgroundColor设置或使用cell.label.layer.backgroundColor,它的作品。

令我困惑的是,在单元格的上下文之外,例如,如果您只是在常规视图中设置标签,则可以设置backgroundColor并仍然看到animation。

这个问题似乎与UILabel。

看来,烦人的是,

你必须设置UILabel的bg颜色清除,然后才可以使用颜色或使其animation。

以下工作很好:

小文字徽章,背景颜色悸动,悸动,悸动:

在故事板中,只需将bg颜色设置为黑色,就可以看到你在做什么。 (在写作时,IBDesignable系统不够好,在故事板中呈现反弹animation。)

当然,你可以添加一个@IBInspectable来设置颜色反弹 – 但为什么当红色/橙色是如此的好! 🙂

 @IBDesignable class ColorTextBadge: UILabel { override var text: String? { didSet { print("Text changed from \(oldValue) to \(text)") // very often, you'll want to change the color or whatever // when this happens } } override init(frame: CGRect) { super.init(frame: frame) initialSetup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) initialSetup() } func initialSetup() { // this will only happen ONCE // annoyingly, you have to, in a word, do this to make color bg animations work, on a UILabel: backgroundColor = UIColor.clear // also, shape the corners...easy let r = self.bounds.size.height / 2 let path = UIBezierPath(roundedRect: self.bounds, cornerRadius:r) let mask = CAShapeLayer() mask.path = path.cgPath self.layer.mask = mask } override func layoutSubviews() { super.layoutSubviews() doAnimation() // you will have to restart the animation EACH TIME // the label is shaped by layout. } func doAnimation() { layer.removeAnimation(forKey: "bounce") let bounce = CABasicAnimation(keyPath: "backgroundColor") bounce.fromValue = sfRed.cgColor bounce.toValue = UIColor.orange.cgColor // core magic: let ct = CACurrentMediaTime().truncatingRemainder(dividingBy: 1) bounce.timeOffset = ct bounce.duration = 0.5 bounce.autoreverses = true bounce.repeatCount = Float.greatestFiniteMagnitude bounce.isRemovedOnCompletion = false layer.add(bounce, forKey: "bounce") } } 

还有更多!

当表格视图单元格在iOS中消失时,任何图层animation都会被杀死 – boo!

不幸的是,只有一个办法来解决这个问题 。 诚实,这是唯一的方法。 (.isRemovedOnCompletion在这里没有帮助。)

在你的表格视图中 ,添加

 override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if let c = cell as? ActivityCell { c.cellWillDisplaySignalling() } } 

然后在你的单元class,

class YourCell:UITableViewCell {@IBOutlet var blah:UILabel! @IBOutlet var blah:UILabel! @IBOutlet var aBadge:ColorTextBadge! @IBOutlet var anotherBadge:ColorTextBadge!

 ... override func cellWillDisplaySignalling() { aBadge.doAnimation() anotherBadge.doAnimation() } 

}

也就是说,不幸的是,细胞现在知道的唯一途径就是出现。

只需在该函数中调用“doAnimation”即可。

最后,同步脉冲!

看看在两线核心魔术 doAnimation。 我不能为解释而烦恼,但是试试看, 它看起来以次充好,除非它同步!