SwiftUI中的键盘和动画问题



我有一个简单的动画

Capsule()
.cornerRadius(25)
.scaleEffect(pulsate ? 1 : 1.2)
.animation(Animation.easeInOut(duration: 1).repeatForever(autoreverses: true).speed(1.5))
.foregroundColor(Color.red)
.frame(width: 75, height: 27)
.onAppear{
self.pulsate.toggle()
}

一切都很好,动画也很好。但如果我打开键盘,当我关闭它时,我的视图胶囊开始跳跃,而不仅仅是做缩放效果。我真的不知道为什么以及如何解决谢谢大家

您应该使用value进行动画


import SwiftUI
struct ContentView: View {

@State private var pulsate: Bool = Bool()
@State private var stringOfText: String = String()

var body: some View {
Capsule()
.cornerRadius(25)
.scaleEffect(pulsate ? 1 : 1.2)
.animation(Animation.easeInOut(duration: 1).repeatForever(autoreverses: true).speed(1.5), value: pulsate)  // <<: here!
.foregroundColor(Color.red)
.frame(width: 75, height: 27)
.onAppear{
self.pulsate.toggle()
}

TextField("enter text", text: $stringOfText)


}
}

最新更新