[英]SwiftUI ForEach no animation delay
我最近更新到 Xcode 11.3,现在我的ForEach
语句中以前有效的 animation 延迟失败了。 下面是我的代码的简化版本。
谢谢
import SwiftUI
struct ContentView: View {
@State var show: Bool = false
var transition: AnyTransition {
let insertion = AnyTransition.move(edge: .trailing)
let removal = AnyTransition.move(edge: .leading)
return .asymmetric(insertion: insertion, removal: removal)
}
var body: some View {
VStack {
Button(action: { withAnimation { self.show.toggle() } } ) {
Text("start animation")
}
if show == true {
test()
.transition(transition)
}
}
}
}
struct wordArray: Identifiable{
var id = UUID()
var words: String
}
struct test: View{
let circleArray = [
wordArray(words: "This"),
wordArray(words: "Should"),
wordArray(words: "Be"),
wordArray(words: "Delayed"),
]
var body: some View{
VStack{
ForEach(circleArray) { wordArray in
Text("\(wordArray.words)")
.animation(Animation.easeInOut.delay(0.5))
}
Text("like")
.animation(Animation.easeInOut.delay(0.5))
Text("This")
.animation(Animation.easeInOut.delay(1))
}.frame(width: UIScreen.main.bounds.width)
}
}
这可能是由于双 VStack。 您可以在VStack
更改为Group
。
Group{
ForEach(circleArray) { wordArray in
Text("\(wordArray.words)")
.animation(Animation.easeInOut.delay(0.5))
}
Text("like")
.animation(Animation.easeInOut.delay(0.5))
Text("This")
.animation(Animation.easeInOut.delay(1))
}.frame(width: UIScreen.main.bounds.width)
在许多情况下,我找到了与ForEach
内的视图不支持过渡或动画相关的问题的解决方案。
第一个解决方案是将项目包含在List
视图中的ForEach
中。
另一种选择是将视图组存储在ForEach
中的ScrollView
中,这是我的首选选项,因为列表视图在渲染内容和方式方面有很多限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.