如何在 SwiftUI 中制作边框颜色变化动画。
这是 UIKit 的代码
extension UIButton{
func blink(setColor: UIColor, repeatCount: Float, duration: Double) {
self.layer.borderWidth = 1.0
let animation: CABasicAnimation = CABasicAnimation(keyPath: "borderColor")
animation.fromValue = UIColor.clear.cgColor
animation.toValue = setColor.cgColor
animation.duration = duration
animation.autoreverses = true
animation.repeatCount = repeatCount
self.layer.borderColor = UIColor.clear.cgColor
self.layer.add(animation, forKey: "")
}
}
我在使用 SwiftUI 项目实现重复文本时遇到了类似的问题。答案看起来太先进了,我无法实施。经过一番搜索和研究。我设法反复闪烁我的文字。对于稍后看到这篇文章的人,您可以使用以下方法尝试此方法withAnimation{}
and .animation()
.
Swift 5
@State private var myRed = 0.2
@State private var myGreen = 0.2
@State private var myBlue = 0.2
var body:some View{
Button(action:{
//
}){
Text("blahblahblah")
}
.border(Color(red: myRed,green: myGreen,blue: myBlue))
.onAppear{
withAnimation{
myRed = 0.5
myGreen = 0.5
myBlue = 0
}
}
.animation(Animation.easeInOut(duration:2).repeatForever(autoreverses:true))
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)