Skip to content

Commit

Permalink
Merge pull request #333 from SDWebImage/bugfix/webimage_isAnimating_b…
Browse files Browse the repository at this point in the history
…inding

Allows easy to use WebImage with `isAnimating` default to false and change to true later
  • Loading branch information
dreampiggy authored Aug 29, 2024
2 parents 5d462f7 + 7efdf22 commit 09dfa5a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 18 additions & 0 deletions Example/SDWebImageSwiftUIDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")!
Expand Down
12 changes: 6 additions & 6 deletions SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public struct WebImage<Content> : 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<Bool> = .constant(true)) where Content == Image {
self.init(url: url, options: options, context: context, isAnimating: isAnimating) { phase in
phase.image ?? Image(platformImage: .empty)
Expand All @@ -132,11 +132,11 @@ public struct WebImage<Content> : 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
Expand Down

0 comments on commit 09dfa5a

Please sign in to comment.