出于某种原因,这对我不起作用:
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");
我没有得到任何动画。
答案 0 :(得分:8)
您必须使用相同的密钥名称。在设置动画之前,您也忘了为图层添加边框宽度和颜色。试试这样:
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")
}
答案 1 :(得分:3)
Swift 4 UIView
扩展名:
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
}
}
然后通过编写来使用它:
myView.animateBorderColor(toColor: .red, duration: 0.5)
答案 2 :(得分:2)
我不知道为什么,但出于某种原因打电话:
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");
然后事情开始按预期工作了。
答案 3 :(得分:1)
(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
答案 4 :(得分:0)
为此,我为CALayer
创建了一个 Swift 4 功能扩展,您可以向其中传递开始UIColor
和结束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")
}
以及动画的持续时间:
borderWidth
请注意,要执行此操作,您应该已经设置了animateBorderColor
,然后调用yourView.layer.borderWidth = 1.5
yourView.layer.animateBorderColor(from: UIColor.green, to: UIColor.red, withDuration: 2.0)
:
<Toolbar className={classes.toolbar}>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.grow}>
Menu
</Typography>
<CardMedia
className={classes.media}
src='logo.svg'
title="Logo"
/>
</Toolbar>
</AppBar>