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.
I don't know why, but for some reason calling:
doesn't work. The color isn't being read correctly or something. I changed it to:
And then things started working as expected.
Swift 4
UIView
extension:And then just use it by writing:
I created a Swift 4 function extension to
CALayer
for this, to which you can pass the starting and endingUIColor
s as well as the duration for the animation:Note that for this to work, you should have already set a
borderWidth
and then callanimateBorderColor
:You have to use the same key name. You also forgot to add a border width and color to your layer before animating it. Try like this: