Explorar el Código

Merge pull request #1301 from onevcat/fix/swiftui-animation

Remove animation setting based on options
Wei Wang hace 6 años
padre
commit
4721bfc5a4

+ 48 - 29
Demo/Demo/Kingfisher-SwiftUI-Demo/Views/SwiftUIList.swift

@@ -24,6 +24,7 @@
 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 //  THE SOFTWARE.
 
+import Kingfisher
 import KingfisherSwiftUI
 import SwiftUI
 
@@ -33,41 +34,59 @@ struct SwiftUIList : View {
 
     var body: some View {
         List(index) { i in
+            ListCell(index: i)
+        }.navigationBarTitle(Text("SwiftUI List"), displayMode: .inline)
+    }
+
+    struct ListCell: View {
+
+        @State var done = false
+
+        var alreadyCached: Bool {
+            ImageCache.default.isCached(forKey: url.absoluteString)
+        }
+
+        let index: Int
+        var url: URL {
+            URL(string: "https://github.com/onevcat/Flower-Data-Set/raw/master/rose/rose-\(index).jpg")!
+        }
+
+        var body: some View {
             HStack(alignment: .center) {
                 Spacer()
-                KFImage(
-                    URL(string: "https://github.com/onevcat/Flower-Data-Set/raw/master/rose/rose-\(i).jpg")!,
-                    options: [.transition(.fade(0.4))]
-                )
-                .resizable()
-                .onSuccess { r in
-                    print("Success: \(i) - \(r.cacheType)")
-                }
-                .onFailure { e in
-                    print("Error \(i): \(e)")
-                }
-                .onProgress { downloaded, total in
-                    print("\(downloaded) / \(total))")
-                }
-                .placeholder {
-                    HStack {
-                        Image(systemName: "arrow.2.circlepath.circle")
-                            .resizable()
-                            .frame(width: 50, height: 50)
-                            .padding(10)
-                        Text("Loading...").font(.title)
+                KFImage(url)
+                    .resizable()
+                    .onSuccess { r in
+                        self.done = true
+                        print("Success: \(self.index) - \(r.cacheType)")
+                    }
+                    .onFailure { e in
+                        print("Error \(self.index): \(e)")
+                    }
+                    .onProgress { downloaded, total in
+                        print("\(downloaded) / \(total))")
                     }
-                    .foregroundColor(.gray)
-                    .opacity(0.3)
-                }
-                .cancelOnDisappear(true)
-                .aspectRatio(contentMode: .fit)
-                .cornerRadius(20)
-                .frame(width: 300, height: 300)
+                    .placeholder {
+                        HStack {
+                            Image(systemName: "arrow.2.circlepath.circle")
+                                .resizable()
+                                .frame(width: 50, height: 50)
+                                .padding(10)
+                            Text("Loading...").font(.title)
+                        }
+                        .foregroundColor(.gray)
+                    }
+                    .cancelOnDisappear(true)
+                    .aspectRatio(contentMode: .fit)
+                    .cornerRadius(20)
+                    .frame(width: 300, height: 300)
+                    .opacity(done || alreadyCached ? 1.0 : 0.3)
+                    .animation(.linear(duration: 0.4))
 
                 Spacer()
             }.padding(.vertical, 12)
-        }.navigationBarTitle(Text("SwiftUI List"), displayMode: .inline)
+        }
+
     }
 }
 

+ 0 - 17
Sources/SwiftUI/ImageBinder.swift

@@ -51,23 +51,6 @@ extension KFImage {
 
         @Published var image: KFCrossPlatformImage?
 
-        // Only `.fade` is now supported.
-        var fadeTransitionAnimation: Animation? {
-            #if os(iOS) || os(tvOS)
-            guard let options = (options.map { KingfisherParsedOptionsInfo($0) }) else {
-                return nil
-            }
-            switch options.transition {
-            case .fade(let duration):
-                return .linear(duration: duration)
-            default:
-                return nil
-            }
-            #else
-            return nil
-            #endif
-        }
-
         init(source: Source?, options: KingfisherOptionsInfo?) {
             self.source = source
             self.options = options

+ 0 - 1
Sources/SwiftUI/KFImage.swift

@@ -87,7 +87,6 @@ public struct KFImage: View {
                     .reduce(Image(crossPlatformImage: binder.image!)) {
                         current, config in config(current)
                     }
-                    .animation(binder.fadeTransitionAnimation)
             } else {
                 Group {
                     if placeholder != nil {