![](/img/trans.png)
[英]CABasicAnimation used for a chronometer animation doesn't work with iOS 8+
[英]CABasicAnimation in uitableviewcell doesn't seem to work
我正在嘗試使用 CABasicAnimation 在 tableview 單元格中更改背景顏色中的標簽,但它似乎不起作用 - 單元格背景顏色保持純色,沒有動畫。 此代碼在 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;
}
請注意,iOS 有一個 UILabel 的已知問題:
這只是關於 iOS 的事情之一。
以下效果很好:
在故事板中,只需將背景顏色設置為黑色,這樣您就可以看到自己在做什么。 (在編寫時,IBDesignable 系統還不夠好,無法在故事板中渲染反彈動畫。)
當然,您可以添加一個 @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")
}
}
還有更多!
不幸的是,只有一種方法可以解決這個問題。 老實說,這是唯一的辦法。 (.isRemovedOnCompletion 在這里沒有幫助。)
在您的表視圖中,添加
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let c = cell as? ActivityCell {
c.cellWillDisplaySignalling()
}
}
然后在你的細胞課上,
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 中查看兩行core magic 。 我懶得解釋,但不管有沒有那個,都可以嘗試一下; 除非同步,否則它看起來很粗糙!
我想出了問題,雖然我不確定為什么會這樣。 如果其他人可以添加到這個答案,請隨意。
看起來好像設置單元格標簽的背景顏色隱藏了圖層的動畫。 如果我注釋掉標簽的 backgroundColor 設置或使用 cell.label.layer.backgroundColor,它就可以工作。
讓我感到困惑的是,在單元格的上下文之外,例如,如果您只是在常規視圖中設置標簽,則可以設置 backgroundColor 並且仍然可以看到動畫。
這個問題是在 5 年前提出的,但仍然有人可能會使用這個答案,所以只需在 UITableViewCell 上執行以下操作。 這應該有效。
override func layoutSubviews() {
super.layoutSubviews()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.doAnimation()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.