如何在 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: "")
}
}
Run Code Online (Sandbox Code Playgroud) 当变量值改变时,我使用 sink 方法调用函数。代码适用于 iOS 13.2.2 但不适用于 iOS 13.3。当 segmentedSelected 变量改变时,函数 segmentedChanged 不会被调用。
public class ChooseViewModel: ObservableObject {
@Published var segmentedSelected = Int()
init() {
_ = $segmentedSelected
.debounce(for: .seconds(0.1), scheduler: DispatchQueue.main)
.sink(receiveValue: self.segmentedChanged(indexValue:))
}
func segmentedChanged(indexValue segIndex: Int) {
print(segIndex)
}
}
Run Code Online (Sandbox Code Playgroud)