swift - 链式动画扰乱了方向变化

标签 swift animation layout swiftui

当我改变方向时,闪烁的 SF 符号开始上下移动。向上和向下动画应该只实现一次(在外观上)。我尝试过使用verticalSizeClass,但是这没有帮助。

工作原理:在外观上,文本和 SF 符号向上移动,然后 Sparkle SF 符号向上和向下缩放。

这是它在方向改变时的外观和行为:

enter image description here

struct ContentView: View {
    @State private var offsetY: CGFloat = 150
    @State private var opacityAmount: Double = 0
    @State private var animationAmount: CGFloat = 1
    
    var body: some View {
        VStack {
            HStack {
                Image(systemName: "sparkle").foregroundColor(.yellow)
                    .offset(y: offsetY)
                    .opacity(opacityAmount)
                    .animation(
                        Animation.easeOut(duration: 0.5).delay(0.1)
                    )
                    .scaleEffect(animationAmount)
                    .animation(Animation.easeInOut(duration: 1).repeatForever(autoreverses: true))
                Text("All new design").font(.largeTitle)
                    .offset(y: offsetY)
                    .opacity(opacityAmount)
                    .animation(
                        Animation.easeOut(duration: 0.5).delay(0.1)
                    )
            }
            .onAppear {
                offsetY = 0
                opacityAmount = 0.8
                animationAmount = 2
            }
        }
    }
}

最佳答案

您没有根据我之前的答案改编动画值连接,并观察其结果。

这是因为 .animation 修饰符引入了隐式动画,该动画应用于其上方的所有可动画 View 修饰符,最终可能会出现不需要的累积多个动画(当然,有时这正是需要的)。

这是固定部分。使用 Xcode 12/iOS 14 进行测试

HStack {
    Image(systemName: "sparkle").foregroundColor(.yellow)
        .offset(y: offsetY)
        .opacity(opacityAmount)
        .animation(
            Animation.easeOut(duration: 0.5).delay(0.1)
        , value: opacityAmount)                     // << here !!
        .scaleEffect(animationAmount)
        .animation(Animation.easeInOut(duration: 1).repeatForever(autoreverses: true),
            value: animationAmount)                // << here !!
    Text("All new design").font(.largeTitle)
        .offset(y: offsetY)
        .opacity(opacityAmount)
        .animation(
            Animation.easeOut(duration: 0.5).delay(0.1)
        , value: opacityAmount)                           // << here !!
}

注意:您可以使用哪个动画来赋予或不赋予显式值。

关于swift - 链式动画扰乱了方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63556043/

相关文章:

swift - Firebase (3.2.0) 使用 Swift (2.0) 查询的确切方式是什么?

javascript - CreateJS 如何对矩形的宽度和高度进行动画处理?

java - GridBagLayout 元素不移动

ios - 在 Firebase 上保存和检索图像

swift - 为什么 Array 的元素在这种情况下是可选的?

google-chrome - CSS3 兼容性问题

javascript - HTML5 canvas 绘制多个在 Canvas 中移动的矩形

css - 允许 <div> 中的图像比同一 <div> 中的文本行宽

css - 是否可以在 html5 页面中使底部边框自动调整大小(根据最后一行的文本宽度)?

ios - Tab Bar 和 UISearchController 出现黑屏