[英]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.