@@ -16,11 +16,15 @@ import SwiftUI
16
16
@available ( iOS 14 . 0 , macOS 11 . 0 , tvOS 14 . 0 , watchOS 7 . 0 , * )
17
17
public class AnimatedImageViewWrapper : PlatformView {
18
18
/// The wrapped actual image view, using SDWebImage's aniamted image view
19
- public var wrapped = SDAnimatedImageView ( )
19
+ @objc dynamic public var wrapped = SDAnimatedImageView ( )
20
+ var observation : NSKeyValueObservation ?
20
21
var interpolationQuality = CGInterpolationQuality . default
21
22
var shouldAntialias = false
22
23
var resizingMode : Image . ResizingMode ?
23
- var imageSize : CGSize ?
24
+
25
+ deinit {
26
+ observation? . invalidate ( )
27
+ }
24
28
25
29
public override func draw( _ rect: CGRect ) {
26
30
#if os(macOS)
@@ -50,15 +54,7 @@ public class AnimatedImageViewWrapper : PlatformView {
50
54
51
55
public override var intrinsicContentSize : CGSize {
52
56
/// Match the behavior of SwiftUI.Image, only when image is resizable, use the super implementation to calculate size
53
- var contentSize = wrapped. intrinsicContentSize
54
- /// Sometimes, like during the transaction, the wrapped.image == nil, which cause contentSize invalid
55
- /// Use image size as backup
56
- /// TODO: This mixed use of UIKit/SwiftUI animation will cause visial issue because the intrinsicContentSize during animation may be changed
57
- if let imageSize = imageSize {
58
- if contentSize != imageSize {
59
- contentSize = imageSize
60
- }
61
- }
57
+ let contentSize = wrapped. intrinsicContentSize
62
58
if let _ = resizingMode {
63
59
/// Keep aspect ratio
64
60
if contentSize. width > 0 && contentSize. height > 0 {
@@ -77,11 +73,17 @@ public class AnimatedImageViewWrapper : PlatformView {
77
73
public override init ( frame frameRect: CGRect ) {
78
74
super. init ( frame: frameRect)
79
75
addSubview ( wrapped)
76
+ observation = observe ( \. wrapped. image, options: [ . new] ) { _, _ in
77
+ self . invalidateIntrinsicContentSize ( )
78
+ }
80
79
}
81
80
82
81
public required init ? ( coder: NSCoder ) {
83
82
super. init ( coder: coder)
84
83
addSubview ( wrapped)
84
+ observation = observe ( \. wrapped. image, options: [ . new] ) { _, _ in
85
+ self . invalidateIntrinsicContentSize ( )
86
+ }
85
87
}
86
88
}
87
89
0 commit comments