new*_*dev 3 animation listview ios swiftui
我正在尝试对点列表的内容进行动画处理。该列表按从最高到最低的顺序排序,因此它会重新组织,因为较低的值高于上面的行。通过在列表上使用,动画效果很好.animation(.default),但是,当我打开视图时,它也会为整个列表设置动画。整个列表就浮现到位了。我希望列表是静态的,只有行在需要重新排序时移动
List {
ForEach(players) { player in
Text(player.score)
}
}.animation(.default)
Run Code Online (Sandbox Code Playgroud)
要仅在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()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4117 次 |
| 最近记录: |