Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SwiftUI: prevent resizable() animation

Tags:

ios

swift

swiftui

I have a cell with an image. The image is downloaded from the internet, and I would like to have a fade opacity transition when the image is downloaded. This is easily accomplished with the couple of modifiers:

.transition(.opacity)
.animation(.default)

But because my image has also the .resizable() modifier I also get a scaling animation that I really don't want.

How can I prevent this?

struct GridCellView: View {

    @ObservedObject var model: CellViewModel

    var body: some  View {
        GeometryReader { proxy in
            Image(uiImage: self.model.image)
                .resizable()
                .transition(.opacity)
                .animation(.default)
                .scaledToFill()
                .frame(width: proxy.size.width, height: proxy.size.width)
                .aspectRatio(1/1, contentMode: .fit)
                .clipped()
                .animation(nil)
        }
    }

}
like image 255
Andrea Miotto Avatar asked Oct 16 '22 05:10

Andrea Miotto


1 Answers

Use .animation(nil) right after property/ies which animations you want to disable, like below

var body: some  View {
    GeometryReader { proxy in
        Image(uiImage: self.model.image)
            .resizable()
            .animation(nil)           // << disables animation
            .transition(.opacity)
            .animation(.default)      // << enables animation
            .scaledToFill()
            .frame(width: proxy.size.width, height: proxy.size.width)
            .aspectRatio(1/1, contentMode: .fit)
            .clipped()
    }
}
like image 158
Asperi Avatar answered Oct 19 '22 05:10

Asperi