Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows easy to use WebImage with isAnimating default to false and change to true later #333

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading