@@ -48,6 +48,46 @@ public struct AnimatedImage : PlatformViewRepresentable {
48
48
var webOptions : SDWebImageOptions = [ ]
49
49
var webContext : [ SDWebImageContextOption : Any ] ? = nil
50
50
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
+
51
91
#if os(macOS)
52
92
public typealias NSViewType = AnimatedImageViewWrapper
53
93
#else
@@ -89,6 +129,13 @@ public struct AnimatedImage : PlatformViewRepresentable {
89
129
}
90
130
}
91
131
}
132
+ if self . isAnimating != view. wrapped. isAnimating {
133
+ if self . isAnimating {
134
+ view. wrapped. startAnimating ( )
135
+ } else {
136
+ view. wrapped. stopAnimating ( )
137
+ }
138
+ }
92
139
93
140
configureView ( view, context: context)
94
141
layoutView ( view, context: context)
@@ -310,30 +357,6 @@ extension AnimatedImage {
310
357
}
311
358
}
312
359
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
-
337
360
#if DEBUG
338
361
struct AnimatedImage_Previews : PreviewProvider {
339
362
static var previews : some View {
0 commit comments