uitableviewcell 中的 CABasicAnimation 似乎不起作用 [英] CABasicAnimation in uitableviewcell doesn't seem to work

查看:25
本文介绍了uitableviewcell 中的 CABasicAnimation 似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 CABasicAnimation 在 tableview 单元格中更改背景颜色,但它似乎不起作用 - 单元格背景颜色保持纯色,没有动画.此代码在 cellForRowAtIndexPath 方法中

I'm trying to make a label within a tableview cell change background color with a CABasicAnimation and it doesn't seem to be working - the cell background color remains solid with no animation. This code is in the cellForRowAtIndexPath method

- (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:

Note that iOS has a known problem with UILabel:

这只是 iOS 的其中一件事情.

It's just one of those things about iOS.

以下效果很好:

在故事板中,只需将背景颜色设置为黑色,这样您就可以看到自己在做什么.(在撰写本文时,IBDesignable 系统还不够好,无法在故事板中渲染反弹动画.)

In storyboard, simply set the bg color to say black just so you can see what you're doing. (The IBDesignable system is not good enough, as of writing, to render the bounce animation in storyboard.)

当然,您可以添加一个@IBInspectable 来设置颜色反弹 - 但是为什么当红色/橙色如此好时!?:)

Naturally, you can add an @IBInspectable just to set the color bounce - but why when red/orange is so good!? :)

@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 在这里没有帮助.)

Unfortunately there is only one way to deal with this. Honest, it's the only way. (.isRemovedOnCompletion does not help here.)

在您的表格视图中,添加

In your table view, add

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

然后在您的单元格类中,

Then in your cell class,

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

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()
}

}

不幸的是,这是细胞现在知道它出现的唯一方法.

That is, unfortunately, the only way for now for a cell to know it is appearing.

只需在该函数中调用doAnimation"即可.

Simply call the "doAnimation"s in that function.

在 doAnimation 中查看两行 core magic.我懒得解释,但不管有没有那个,都可以尝试一下;除非同步,否则它看起来很简陋!

Look in doAnimation at the two lines core magic. I can't be bothered explaining, but try it with and without that; it looks shoddy unless it is synced!

这篇关于uitableviewcell 中的 CABasicAnimation 似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆