如何在swift中设置边框颜色更改的动画

如何在swift中设置边框颜色更改的动画,swift,cabasicanimation,Swift,Cabasicanimation,出于某种原因,这对我不起作用: 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 an

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

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

我不会出现任何动画。

您必须使用相同的关键点名称。在设置图层动画之前,您还忘了为图层添加边框宽度和颜色。试着这样做:

let color = CABasicAnimation(keyPath: "borderColor")

@IBAction func animateBorder(sender: AnyObject) {
    color.fromValue = UIColor.greenColor().CGColor
    color.toValue = UIColor.redColor().CGColor
    color.duration = 2
    color.repeatCount = 1
    sender.layer.borderWidth = 2
    sender.layer.borderColor = UIColor.greenColor().CGColor
    sender.layer.addAnimation(color, forKey: "borderColor")
}

我不知道为什么,但出于某种原因打电话:

color.fromValue = sender.layer.borderColor
不起作用。颜色没有被正确读取或是其他什么。我把它改成:

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

然后事情开始按预期进行。

我为此创建了一个Swift 4函数扩展,扩展到
CALayer
,您可以将开始和结束的
UIColor
s以及动画的持续时间传递给它:

extension CALayer {

func animateBorderColor(from startColor: UIColor, to endColor: UIColor, withDuration duration: Double) {
    let colorAnimation = CABasicAnimation(keyPath: "borderColor")
    colorAnimation.fromValue = startColor.cgColor
    colorAnimation.toValue = endColor.cgColor
    colorAnimation.duration = duration
    self.borderColor = endColor.cgColor
    self.add(colorAnimation, forKey: "borderColor")
}
请注意,要使其工作,您应该已经设置了
边框宽度
,然后调用
animateBorderColor

yourView.layer.borderWidth = 1.5
yourView.layer.animateBorderColor(from: UIColor.green, to: UIColor.red, withDuration: 2.0)

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 5、Xcode 11、iOS 13)

对于任何想同时更改边框颜色和宽度的人来说,下面的代码对我很有用&动画看起来非常平滑。也许其他人知道如何将两者结合为一

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

玩了一会儿之后。我认为问题在于颜色。它没有读取sender.layer.borderColor.color.fromValue=sender.layer.borderColor也适用于我这里的问题是密钥名forKey:“颜色和宽度”。如果要设置颜色和边框的动画,需要为调用sender.layer.addAnimation(color,forKey:“color”)的每个属性(borderColor和borderWidth)创建一个CABasicAnimation。只有当我将其更改为color.fromValue=sender.layer.borderColor时,事情才会中断。我不明白,但这就是正在发生的事情。这段代码给出了什么结果?什么不起作用?在编辑您的问题时包含错误消息和预期结果(不要评论评论,也不要在详细阐述中包含编辑或更新,我们可以查看编辑历史记录中的更改)。要组合动画,我们可以使用CAAnimationGroup。让animationGroup=CAAnimationGroup()animationGroup.animations=[colorsAnimation]animationGroup.duration=duration layer.add(animationGroup,forKey:“动画组”)