How to animate borderColor change in swift

后端 未结 5 1001
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  萌比男神i
    2021-01-18 06:07

    Swift 4 UIView extension:

    extension UIView {
      func animateBorderColor(toColor: UIColor, duration: Double) {
        let animation:CABasicAnimation = CABasicAnimation(keyPath: "borderColor")
        animation.fromValue = layer.borderColor
        animation.toValue = toColor.cgColor
        animation.duration = duration
        layer.add(animation, forKey: "borderColor")
        layer.borderColor = toColor.cgColor
      }
    }
    

    And then just use it by writing:

    myView.animateBorderColor(toColor: .red, duration: 0.5)
    

提交回复
热议问题