gpt4 book ai didi

swift - 如何在 swift 中为 borderColor 变化设置动画

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:46 26 4
gpt4 key购买 nike

出于某种原因,这对我不起作用:

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");

我没有让任何动画发生。

最佳答案

Swift 4 UIView 扩展:

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

然后通过编写来使用它:

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

关于swift - 如何在 swift 中为 borderColor 变化设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28934948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com