Skip to content

Commit 1b68c49

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 1b68c49

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

Diff for: 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")!

Diff for: SDWebImageSwiftUI/Classes/WebImage.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -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)