Search code examples
swiftcabasicanimation

How to animate borderColor change in swift


For some reason this isn't working for me:

let color = CABasicAnimation(keyPath: "borderColor")
color.fromValue = sender.layer.borderColor;
color.toValue = UIColor.redColor().CGColor;
color.duration = 2;
color.repeatCount = 1;
sender.layer.addAnimation(color, forKey: "color and width");

I'm not getting any animation to occur.


Solution

  • I don't know why, but for some reason calling:

    color.fromValue = sender.layer.borderColor
    

    doesn't work. The color isn't being read correctly or something. I changed it to:

    let color = CABasicAnimation(keyPath: "borderColor");
    color.fromValue = UIColor.greenColor().CGColor;
    color.toValue = UIColor.redColor().CGColor;
    color.duration = 2;
    color.repeatCount = 1;
    sender.layer.addAnimation(color, forKey: "color and width");
    

    And then things started working as expected.