Search code examples
animationswiftui

SwiftUI - how to pulsate image opacity?


I have an Image in SwiftUI that I would like to "pulsate" (come & go every second or so) forever.

I tried a number of things but I can't seem to get the effect I want. One of the things I tried is the code below (which seems to do nothing! :) ).

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

Any ideas?


Solution

  • Here is an approach. Tested with Xcode 11.4 / iOS 13.4

    demo

    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 }
        }
    }