如何在SwitfUI中制作简单的上下浮动动画?

Sus*_*dan 3 animation ios swift swiftui

我想制作文字“你好,世界!” 一旦景色出现,就慢慢地、不停地上下浮动。

import SwiftUI

struct animate: View {
    var body: some View {
        Text("Hello, World!")
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢你!

Asp*_*eri 5

更新:Xcode 13.4 / iOS 15.5

这是可能的简单方法。使用 Xcode 11.4 / iOS 13.4 进行测试。当然参数可以根据需要进行调整。

演示

struct BoncingView: View {
    @State private var bouncing = false
    var body: some View {
        Text("Hello, World!")
            .frame(maxHeight: .infinity, alignment: bouncing ? .bottom : .top)
            .animation(Animation.easeInOut(duration: 5.0).repeatForever(autoreverses: true), value: bouncing)
            .onAppear {
                self.bouncing.toggle()
            }
    }
}
Run Code Online (Sandbox Code Playgroud)