SwiftUI - 如何脉动图像不透明度?

Ger*_*ard 3 animation swiftui

我在 SwiftUI 中有一个图像,我希望它永远“脉动”(每秒左右来来去去)。

我尝试了很多方法,但似乎无法达到我想要的效果。我尝试过的事情之一是下面的代码(它似乎什么也没做!:))。

Image(systemName: "dot.radiowaves.left.and.right" )
.foregroundColor(.blue)
.transition(.opacity)
.animation(Animation.easeInOut(duration: 1)
    .repeatForever(autoreverses: true))
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Asp*_*eri 9

这是一种方法。使用 Xcode 11.4 / iOS 13.4 进行测试

演示

struct DemoImagePulsate: View {
    @State private var value = 1.0
    var body: some View {
        Image(systemName: "dot.radiowaves.left.and.right" )
            .foregroundColor(.blue)
            .opacity(value)
            .animation(Animation.easeInOut(duration: 1).repeatForever(autoreverses: true))
        .onAppear { self.value = 0.3 }
    }
}
Run Code Online (Sandbox Code Playgroud)