The picture will disappear in 10-15 seconds. SwiftUi

How to make the picture disappear 15 seconds after opening the application?

which method should i use? Animation or some kind of function?

My code:

struct Img: View {
    var body: some View {
        
        VStack {
            
            Image(systemName: "photo.fill")
                .resizable()
                .scaledToFit()
                .frame(width: 200, height: 200)
            
        }
    }
}

after 10 -15 seconds after opening the application, the picture disappears and does not appear. After I reopen the application, there will be a picture again and it will disappear after 10 – 15 seconds …

Help me if you know how to do this, I will be grateful for any help

>Solution :

You can use an animation delay.

This example will show the image for 15 seconds, then fade out. When the application is force quit and opened again, the same thing happens again:

struct Img: View {
    @State private var isImageVisible = true

    var body: some View {
        VStack {
            Image(systemName: "photo.fill")
                .resizable()
                .scaledToFit()
                .frame(width: 200, height: 200)
                .opacity(isImageVisible ? 1 : 0)
                .animation(.default.delay(15), value: isImageVisible)
        }
        .onAppear {
            isImageVisible = false
        }
    }
}

Leave a Reply