[英]How to animate hideable views with SwiftUI?
我正在试用 SwiftUI,虽然我发现它的许多功能非常优雅,但我在动画和过渡方面遇到了麻烦。 目前,我有类似的东西
if shouldShowText { Text(str).animation(.default).transition(AnyTransition.opacity.animation(.easeInOut)) }
这个标签可以正确转换,但是当它应该移动时(例如,当上面的另一个视图被隐藏时)它不会像我预期的那样动画,而是跳到位。 我注意到将所有内容都包装在HStack
有效的,但我不明白为什么这是必要的,我希望有更好的解决方案。
谢谢
如果我正确理解并重建了您的场景,您需要使用显式withAnimation
(取决于“上方视图”或两者的需要),如下所示
struct SimpleTest: View {
@State var shouldShowText = false
@State var shouldShowAbove = true
var body: some View {
VStack {
HStack
{
Button("ShowTested") { withAnimation { self.shouldShowText.toggle() } }
Button("HideAbove") { withAnimation { self.shouldShowAbove.toggle() } }
}
Divider()
if shouldShowAbove {
Text("Just some above text").padding()
}
if shouldShowText {
Text("Tested Text").animation(.default).transition(AnyTransition.opacity.animation(.easeInOut))
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.