animation - swiftui,动画应用于父效果子动画

标签 animation swiftui

RectangleView 有滑动动画,他的子 TextView 有旋转动画。我想 RectangleView 和他的 child (TextView)作为一个整体滑入屏幕(easeInOut)当 Go!按下,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 }
    }
}

最佳答案

如果有多个同步动画,通用解决方案(在大多数情况下)是为每个动画使用显式状态值。
所以这是一个更正的代码(使用 Xcode 12.1/iOS 14.1 测试,使用模拟器或设备,预览会错误地呈现一些过渡)
demo

struct AnimationTestView: View {
    @State private var go = false
    var body: some View {
        VStack {
            Button("Go!") {
                go.toggle()
            }
                VStack {      // container needed for correct transition !!
                    if go {
                         RectangleView()
                              .transition(.slide)
                    }
                }.animation(.easeInOut, value: go)    // << here !!
        }.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, value: animationRotating)          // << here !!
            .onAppear { animationRotating = true }
            .onDisappear { animationRotating = false }
    }
}

关于animation - swiftui,动画应用于父效果子动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65582121/

相关文章:

android - 如何通过使 ImageView 变灰来模拟按下按钮?

iphone - Cocos2D Director暂停/恢复问题

ios - UILabel 的 setAlpha 动画不起作用

ios - 如何在 ios 的方形应用程序中创建相同的动画?

ios - 如何在 iOS 14、SwiftUI 2 中以编程方式更改 PageTabView?

Swift 类型 'any View' 无法符合 'View' 协议(protocol)

android - 使用图像的椭圆路径动画

ios - SwiftUI:删除ForEach中的最后一行

ios - 在 SwiftUI 的弹出窗口中保存核心数据实体会抛出 nilError,而无需再次将 .environment 传递给 SubView

SwiftUI:只能编辑 ForEach 列表中的第一项