From 7efdf228f68073ec9a95a2308378fae82932f10c Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 28 Aug 2024 17:35:43 +0800 Subject: [PATCH] Allows easy to use WebImage with `isAnimating` default to false and change to true later Since SDAnimatedImage has fallback logic, we can apply this to WebImage by default without dynamic check --- .../SDWebImageSwiftUIDemo/ContentView.swift | 18 ++++++++++++++++++ SDWebImageSwiftUI/Classes/WebImage.swift | 12 ++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Example/SDWebImageSwiftUIDemo/ContentView.swift b/Example/SDWebImageSwiftUIDemo/ContentView.swift index 4cb0fa5..17e43e7 100644 --- a/Example/SDWebImageSwiftUIDemo/ContentView.swift +++ b/Example/SDWebImageSwiftUIDemo/ContentView.swift @@ -17,6 +17,24 @@ class UserSettings: ObservableObject { #endif } +struct ContentView5: View { + let url: URL = URL(string: "http://assets.sbnation.com/assets/2512203/dogflops.gif")! + + @State private var isAnimating = false + + var body: some View { + ZStack { + WebImage(url: url, isAnimating: $isAnimating) + .pausable(false) + Button { + isAnimating.toggle() + } label: { + Text(isAnimating ? "Stop" : "Start") + } + } + } +} + #if !os(watchOS) struct ContentView4: View { var url = URL(string: "https://github.com/SDWebImage/SDWebImageSwiftUI/assets/97430818/72d27f90-e9d8-48d7-b144-82ada828a027")! diff --git a/SDWebImageSwiftUI/Classes/WebImage.swift b/SDWebImageSwiftUI/Classes/WebImage.swift index 3dab6a3..59fe49a 100644 --- a/SDWebImageSwiftUI/Classes/WebImage.swift +++ b/SDWebImageSwiftUI/Classes/WebImage.swift @@ -109,7 +109,7 @@ public struct WebImage : View where Content: View { /// - Parameter scale: The scale to use for the image. The default is 1. Set a different value when loading images designed for higher resolution displays. For example, set a value of 2 for an image that you would name with the @2x suffix if stored in a file on disk. /// - Parameter options: The options to use when downloading the image. See `SDWebImageOptions` for the possible values. /// - Parameter context: A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold. - /// - Parameter isAnimating: The binding for animation control. The binding value should be `true` when initialized to setup the correct animated image class. If not, you must provide the `.animatedImageClass` explicitly. When the animation started, this binding can been used to start / stop the animation. + /// - Parameter isAnimating: The binding for animation control. When the animation started, this binding can been used to start / stop the animation. You can still customize the `.animatedImageClass` context for advanced custom animation. public init(url: URL?, scale: CGFloat = 1, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding = .constant(true)) where Content == Image { self.init(url: url, options: options, context: context, isAnimating: isAnimating) { phase in phase.image ?? Image(platformImage: .empty) @@ -132,11 +132,11 @@ public struct WebImage : View where Content: View { if context[.imageScaleFactor] == nil { context[.imageScaleFactor] = scale } - // provide animated image class if the initialized `isAnimating` is true, user can still custom the image class if they want - if isAnimating.wrappedValue { - if context[.animatedImageClass] == nil { - context[.animatedImageClass] = SDAnimatedImage.self - } + // always provide animated image class to allows dynamic control + // since most cases, SDAnimatedImage should be compatible with UIImage + // user can still custom the image class if they want + if context[.animatedImageClass] == nil { + context[.animatedImageClass] = SDAnimatedImage.self } let imageModel = WebImageModel() imageModel.url = url