Bläddra i källkod

Merge pull request #1830 from onevcat/fix/force-transition-kfimage

Load image when appear when need force transition
Wei Wang 4 år sedan
förälder
incheckning
fd3c17fb2e

+ 4 - 1
Demo/Demo/Kingfisher-Demo/SwiftUIViews/SingleViewDemo.swift

@@ -32,6 +32,7 @@ struct SingleViewDemo : View {
 
 
     @State private var index = 1
     @State private var index = 1
     @State private var blackWhite = false
     @State private var blackWhite = false
+    @State private var forceTransition = true
 
 
     var url: URL {
     var url: URL {
         URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher-TestImages/master/DemoAppImage/Loading/kingfisher-\(self.index).jpg")!
         URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher-TestImages/master/DemoAppImage/Loading/kingfisher-\(self.index).jpg")!
@@ -52,7 +53,7 @@ struct SingleViewDemo : View {
                     ProgressView(progress)
                     ProgressView(progress)
                 }
                 }
                 .fade(duration: index == 1 ? 0 : 1) // Do not animate for the first image. Otherwise it causes an unwanted animation when the page is shown.
                 .fade(duration: index == 1 ? 0 : 1) // Do not animate for the first image. Otherwise it causes an unwanted animation when the page is shown.
-                .forceTransition()
+                .forceTransition(forceTransition)
                 .resizable()
                 .resizable()
                 .frame(width: 300, height: 300)
                 .frame(width: 300, height: 300)
                 .cornerRadius(20)
                 .cornerRadius(20)
@@ -65,6 +66,8 @@ struct SingleViewDemo : View {
             Button(action: {
             Button(action: {
                 self.blackWhite.toggle()
                 self.blackWhite.toggle()
             }) { Text("Black & White") }
             }) { Text("Black & White") }
+            Toggle("Force Transition?", isOn: $forceTransition)
+                .frame(width: 300)
 
 
         }.navigationBarTitle(Text("Basic Image"), displayMode: .inline)
         }.navigationBarTitle(Text("Basic Image"), displayMode: .inline)
     }
     }

+ 10 - 1
Sources/SwiftUI/KFImageRenderer.swift

@@ -37,7 +37,10 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
     let context: KFImage.Context<HoldingView>
     let context: KFImage.Context<HoldingView>
     
     
     var body: some View {
     var body: some View {
-        binder.start(context: context)
+        if !context.options.forceTransition {
+            binder.start(context: context)
+        }
+        
         return ZStack {
         return ZStack {
             context.configurations
             context.configurations
                 .reduce(HoldingView.created(from: binder.loadedImage, context: context)) {
                 .reduce(HoldingView.created(from: binder.loadedImage, context: context)) {
@@ -52,6 +55,12 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
                         Color.clear
                         Color.clear
                     }
                     }
                 }
                 }
+                .onAppear { [weak binder = self.binder] in
+                    guard let binder = binder else {
+                        return
+                    }
+                    binder.start(context: context)
+                }
                 .onDisappear { [weak binder = self.binder] in
                 .onDisappear { [weak binder = self.binder] in
                     guard let binder = binder else {
                     guard let binder = binder else {
                         return
                         return