How to animate borderColor change in swift

后端 未结 5 969
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 05:26

For some reason this isn\'t working for me:

let color = CABasicAnimation(keyPath: \"borderColor\")
color.fromValue = sender.layer.borderColor;
color.toValue          


        
5条回答
  •  终归单人心
    2021-01-18 06:16

    (Swift 5, Xcode 11, iOS 13)

    For anyone who is wanting to change border color and width at the same time, the following code is working for me & the animation looks very smooth. Maybe someone else knows of a way to combine both into one?

    let borderColorAnimation: CABasicAnimation = CABasicAnimation(keyPath: "borderColor")
    borderColorAnimation.fromValue = layer.borderColor
    borderColorAnimation.toValue = toColor.cgColor
    borderColorAnimation.duration = animationDuration
    layer.add(borderColorAnimation, forKey: "borderColor")
    layer.borderColor = toColor.cgColor
    
    let borderWidthAnimation: CABasicAnimation = CABasicAnimation(keyPath: "borderWidth")
    borderWidthAnimation.fromValue = layer.borderWidth
    borderWidthAnimation.toValue = toWidth
    borderWidthAnimation.duration = animationDuration
    layer.add(borderWidthAnimation, forKey: "borderWidth")
    layer.borderWidth = toWidth
    

提交回复
热议问题