Kingfisher: Transitions not working in SwiftUI

Created on 20 Mar 2020  Â·  3Comments  Â·  Source: onevcat/Kingfisher

Check List

Issue Description

Transitions don't work in SwiftUI.

What

When using any of the .transition options on SwiftUI's KFImage, the transition is not applied. It just instantly shows the image.

Reproduce

struct ContentView: View {
    var body: some View {
        KFImage(URL(string: "https://ev-database.org/img/auto/Tesla_Model_3/Tesla_Model_3-03.jpg")!, options: [.transition(.fade(3))])
    }
}

Other Comment

I've also tried adding .forceTransition, and removing the app (to avoid cached version) with no luck.

Thanks in advance!

Most helpful comment

I tried to implement a way to support fade-in transition option. However, I cannot reach a satisfying result. By adding the .transition or .opacity inside the KFImage, and mark it as am implicit animation with .animation brings quite a lot undesired side-effect to the view. It (the .animation) is too invasive, such as animating all other animatable properties in the view.

I guess unless we can find a better way, we leave the decision and implementation to the user. Basically it should not be that hard. Some wrapper like this would be a good starting point:

@State var done = false

var body: some View {
  KFImage(url, isLoaded: $done)
    .opacity(done ? 1.0 : 0.3)
    .animation(.linear(duration: 0.4))
}

Also check the sample code for KingfisherSwiftUI in this repo on fading effect.

All 3 comments

Some options (like transition) is manipulating UIView properties. In a SwiftUI Image, there is no such property to set now. Instead, maybe you can use a SwiftUI's way to implement it yourself, like this.

Maybe it is a good idea to also make KFImage support this option too. But I doubt if I can find a good way for it now. Let's try!

I tried to implement a way to support fade-in transition option. However, I cannot reach a satisfying result. By adding the .transition or .opacity inside the KFImage, and mark it as am implicit animation with .animation brings quite a lot undesired side-effect to the view. It (the .animation) is too invasive, such as animating all other animatable properties in the view.

I guess unless we can find a better way, we leave the decision and implementation to the user. Basically it should not be that hard. Some wrapper like this would be a good starting point:

@State var done = false

var body: some View {
  KFImage(url, isLoaded: $done)
    .opacity(done ? 1.0 : 0.3)
    .animation(.linear(duration: 0.4))
}

Also check the sample code for KingfisherSwiftUI in this repo on fading effect.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sadeghgoo picture sadeghgoo  Â·  4Comments

rainerjungwirth picture rainerjungwirth  Â·  4Comments

joebenton picture joebenton  Â·  4Comments

wudijimao picture wudijimao  Â·  3Comments

duongdangng picture duongdangng  Â·  5Comments