考虑以下代码,为什么在没有该属性的情况下初始化的视图中的动画n在滚动列表时停止?
在 Xcode 11.3 (11C29) 上使用设备和模拟器上的新默认项目进行了测试。
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
List(1...50, id: \.self) { n in
HStack {
KeepRolling()
Spacer()
KeepRolling(n: n)
}
}
}
}
}
struct KeepRolling: View {
@State var isAnimating = false
var n: Int? = nil
var body: some View {
Rectangle()
.frame(width: 50, height: 50)
.rotationEffect(Angle(degrees: self.isAnimating ? 360 : 0))
.onAppear {
withAnimation(Animation.linear(duration: 2).repeatForever(autoreverses: false)) {
self.isAnimating = true
}
}
} …Run Code Online (Sandbox Code Playgroud)