【发布时间】:2021-01-05 15:49:07
【问题描述】:
RectangleView 有滑动动画,他的子 TextView 有旋转动画。我想当 Go!按下,TextView 永远旋转(线性)。但实际上子TextView与父TextView分离,旋转(线性)和滑动(线性),永远重复。
为什么将动画应用于父效果子动画?
struct AnimationTestView: View {
@State private var go = false
var body: some View {
VStack {
Button("Go!") {
go.toggle()
}
if go {
RectangleView()
.transition(.slide)
.animation(.easeInOut)
}
}.navigationTitle("Animation Test")
}
}
struct RectangleView: View {
var body: some View {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(.pink)
.overlay(TextView())
}
}
struct TextView: View {
@State private var animationRotating: Bool = false
let animation = Animation.linear(duration: 3.0).repeatForever(autoreverses: false)
var body: some View {
Text("Test")
.foregroundColor(.blue)
.rotationEffect(.degrees(animationRotating ? 360 : 0))
.animation(animation)
.onAppear { animationRotating = true }
.onDisappear { animationRotating = false }
}
}
【问题讨论】: