gpt4 book ai didi

ios - 使用 SwiftUI 制作按钮闪烁动画

转载 作者:行者123 更新时间:2023-11-29 05:17:36 25 4
gpt4 key购买 nike

如何在 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{}.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))
}

关于ios - 使用 SwiftUI 制作按钮闪烁动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59015929/

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