Skip to content

Commit 7efdf22

Browse files
committed
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
1 parent 5d462f7 commit 7efdf22

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Example/SDWebImageSwiftUIDemo/ContentView.swift

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ class UserSettings: ObservableObject {
1717
#endif
1818
}
1919

20+
struct ContentView5: View {
21+
let url: URL = URL(string: "http://assets.sbnation.com/assets/2512203/dogflops.gif")!
22+
23+
@State private var isAnimating = false
24+
25+
var body: some View {
26+
ZStack {
27+
WebImage(url: url, isAnimating: $isAnimating)
28+
.pausable(false)
29+
Button {
30+
isAnimating.toggle()
31+
} label: {
32+
Text(isAnimating ? "Stop" : "Start")
33+
}
34+
}
35+
}
36+
}
37+
2038
#if !os(watchOS)
2139
struct ContentView4: View {
2240
var url = URL(string: "https://github.com/SDWebImage/SDWebImageSwiftUI/assets/97430818/72d27f90-e9d8-48d7-b144-82ada828a027")!

SDWebImageSwiftUI/Classes/WebImage.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public struct WebImage<Content> : View where Content: View {
109109
/// - 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.
110110
/// - Parameter options: The options to use when downloading the image. See `SDWebImageOptions` for the possible values.
111111
/// - 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.
112-
/// - 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.
112+
/// - 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.
113113
public init(url: URL?, scale: CGFloat = 1, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding<Bool> = .constant(true)) where Content == Image {
114114
self.init(url: url, options: options, context: context, isAnimating: isAnimating) { phase in
115115
phase.image ?? Image(platformImage: .empty)
@@ -132,11 +132,11 @@ public struct WebImage<Content> : View where Content: View {
132132
if context[.imageScaleFactor] == nil {
133133
context[.imageScaleFactor] = scale
134134
}
135-
// provide animated image class if the initialized `isAnimating` is true, user can still custom the image class if they want
136-
if isAnimating.wrappedValue {
137-
if context[.animatedImageClass] == nil {
138-
context[.animatedImageClass] = SDAnimatedImage.self
139-
}
135+
// always provide animated image class to allows dynamic control
136+
// since most cases, SDAnimatedImage should be compatible with UIImage
137+
// user can still custom the image class if they want
138+
if context[.animatedImageClass] == nil {
139+
context[.animatedImageClass] = SDAnimatedImage.self
140140
}
141141
let imageModel = WebImageModel()
142142
imageModel.url = url

0 commit comments

Comments
 (0)