我正在嘗試為點串列的內容設定影片。該串列從最高到最低排序,因此它會重新組織,因為較低的值高于上面的行。通過在串列上使用 .animation(.default) 影片效果很好,但是,當我打開視圖時,它也會為整個串列設定影片。整個串列浮動到位。我希望串列是靜態的,并且在需要重新排序時只移動行
List {
ForEach(players) { player in
Text(player.score)
}
}.animation(.default)
uj5u.com熱心網友回復:
要僅在State資料中的某些內容發生更改時設定影片,請使用withAnimation您更改它的部分而不是整個串列:
import SwiftUI
struct Player: Identifiable {
var id = UUID()
var score: String
}
struct ContentView: View {
@State var players: [Player] = [
.init(score: "2"),
.init(score: "3"),
.init(score: "6"),
.init(score: "1")]
var body: some View {
VStack {
Button("shuffle") {
withAnimation(.easeIn) {
players.shuffle()
}
}
List {
ForEach(players) { player in
Text(player.score)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438391.html
上一篇:MacOS-swift-CABasicAnimation當前值
下一篇:影片SVG虛線逐漸變為不同的顏色
