@@ -48,6 +48,46 @@ public struct AnimatedImage : PlatformViewRepresentable {
4848 var webOptions : SDWebImageOptions = [ ]
4949 var webContext : [ SDWebImageContextOption : Any ] ? = nil
5050
51+ /// A Binding to control the animation. You can bind external logic to control the animation status.
52+ /// True to start animation, false to stop animation.
53+ @Binding public var isAnimating : Bool
54+
55+ public init ( url: URL ? , placeholder: PlatformImage ? = nil , options: SDWebImageOptions = [ ] , context: [ SDWebImageContextOption : Any ] ? = nil ) {
56+ self . init ( url: url, placeholder: placeholder, options: options, context: context, isAnimating: . constant( true ) )
57+ }
58+
59+ public init ( url: URL ? , placeholder: PlatformImage ? = nil , options: SDWebImageOptions = [ ] , context: [ SDWebImageContextOption : Any ] ? = nil , isAnimating: Binding < Bool > ) {
60+ self . _isAnimating = isAnimating
61+ self . placeholder = placeholder
62+ self . webOptions = options
63+ self . webContext = context
64+ self . imageModel. url = url
65+ }
66+
67+ public init ( name: String , bundle: Bundle ? = nil ) {
68+ self . init ( name: name, bundle: bundle, isAnimating: . constant( true ) )
69+ }
70+
71+ public init ( name: String , bundle: Bundle ? = nil , isAnimating: Binding < Bool > ) {
72+ self . _isAnimating = isAnimating
73+ #if os(macOS)
74+ let image = SDAnimatedImage ( named: name, in: bundle)
75+ #else
76+ let image = SDAnimatedImage ( named: name, in: bundle, compatibleWith: nil )
77+ #endif
78+ self . imageModel. image = image
79+ }
80+
81+ public init ( data: Data , scale: CGFloat = 0 ) {
82+ self . init ( data: data, scale: scale, isAnimating: . constant( true ) )
83+ }
84+
85+ public init ( data: Data , scale: CGFloat = 0 , isAnimating: Binding < Bool > ) {
86+ self . _isAnimating = isAnimating
87+ let image = SDAnimatedImage ( data: data, scale: scale)
88+ self . imageModel. image = image
89+ }
90+
5191 #if os(macOS)
5292 public typealias NSViewType = AnimatedImageViewWrapper
5393 #else
@@ -89,6 +129,13 @@ public struct AnimatedImage : PlatformViewRepresentable {
89129 }
90130 }
91131 }
132+ if self . isAnimating != view. wrapped. isAnimating {
133+ if self . isAnimating {
134+ view. wrapped. startAnimating ( )
135+ } else {
136+ view. wrapped. stopAnimating ( )
137+ }
138+ }
92139
93140 configureView ( view, context: context)
94141 layoutView ( view, context: context)
@@ -310,30 +357,6 @@ extension AnimatedImage {
310357 }
311358}
312359
313- // Initializer
314- extension AnimatedImage {
315- public init ( url: URL ? , placeholder: PlatformImage ? = nil , options: SDWebImageOptions = [ ] , context: [ SDWebImageContextOption : Any ] ? = nil ) {
316- self . placeholder = placeholder
317- self . webOptions = options
318- self . webContext = context
319- self . imageModel. url = url
320- }
321-
322- public init ( name: String , bundle: Bundle ? = nil ) {
323- #if os(macOS)
324- let image = SDAnimatedImage ( named: name, in: bundle)
325- #else
326- let image = SDAnimatedImage ( named: name, in: bundle, compatibleWith: nil )
327- #endif
328- self . imageModel. image = image
329- }
330-
331- public init ( data: Data , scale: CGFloat = 0 ) {
332- let image = SDAnimatedImage ( data: data, scale: scale)
333- self . imageModel. image = image
334- }
335- }
336-
337360#if DEBUG
338361struct AnimatedImage_Previews : PreviewProvider {
339362 static var previews : some View {
0 commit comments