Skip to content

Commit cd85148

Browse files
committed
Using the ZStack as a container to distinguish the container disappear vs frame disapppear
1 parent ba9db28 commit cd85148

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

SDWebImageSwiftUI/Classes/ImagePlayer.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,21 @@ public final class ImagePlayer : ObservableObject {
105105
currentAnimatedImage = animatedImage
106106
if let imagePlayer = SDAnimatedImagePlayer(provider: animatedImage) {
107107
imagePlayer.animationFrameHandler = { [weak self] (index, frame) in
108-
self?.currentFrameIndex = index
109-
self?.currentFrame = frame
108+
guard let self = self else {
109+
return
110+
}
111+
if (self.isPlaying) {
112+
self.currentFrameIndex = index
113+
self.currentFrame = frame
114+
}
110115
}
111116
imagePlayer.animationLoopHandler = { [weak self] (loopCount) in
112-
self?.currentLoopCount = loopCount
117+
guard let self = self else {
118+
return
119+
}
120+
if (self.isPlaying) {
121+
self.currentLoopCount = loopCount
122+
}
113123
}
114124
// Setup configuration
115125
if let maxBufferSize = maxBufferSize {

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,17 @@ public struct WebImage : View {
102102
}
103103

104104
public var body: some View {
105-
return Group {
106-
// Render Logic
105+
// Container
106+
return ZStack {
107+
// This empty Image is used to receive container's level appear/disappear to start/stop player, reduce CPU usage
108+
Image(platformImage: .empty)
109+
.onAppear {
110+
self.appearAction()
111+
}
112+
.onDisappear {
113+
self.disappearAction()
114+
}
115+
// Render Logic for actual animated image frame or static image
107116
if imageManager.image != nil && imageModel.url == imageManager.currentURL {
108117
if isAnimating && !imageManager.isIncremental {
109118
setupPlayer()
@@ -136,8 +145,6 @@ public struct WebImage : View {
136145
}
137146
})
138147
}
139-
}.onDisappear {
140-
self.disappearAction()
141148
}
142149
}
143150

@@ -207,18 +214,20 @@ public struct WebImage : View {
207214
}
208215
}
209216

210-
/// Animated Image Disappear should stop display link
217+
/// Container level to resume animation when appear
218+
func appearAction() {
219+
self.imagePlayer.startPlaying()
220+
}
221+
222+
/// Container level to stop animation when disappear
211223
func disappearAction() {
212-
// Only stop the player which is not intermediate status
213-
if !imagePlayer.isWaiting {
214-
if self.imageConfiguration.pausable {
215-
self.imagePlayer.pausePlaying()
216-
} else {
217-
self.imagePlayer.stopPlaying()
218-
}
219-
if self.imageConfiguration.purgeable {
220-
self.imagePlayer.clearFrameBuffer()
221-
}
224+
if self.imageConfiguration.pausable {
225+
self.imagePlayer.pausePlaying()
226+
} else {
227+
self.imagePlayer.stopPlaying()
228+
}
229+
if self.imageConfiguration.purgeable {
230+
self.imagePlayer.clearFrameBuffer()
222231
}
223232
}
224233

@@ -235,19 +244,15 @@ public struct WebImage : View {
235244
// Bind frame index to ID to ensure onDisappear called with sync
236245
return configure(image: currentFrame)
237246
.id("\(imageModel.url!):\(imagePlayer.currentFrameIndex)")
238-
.onPlatformAppear(appear: {
239-
self.imagePlayer.startPlaying()
240-
}, disappear: {
241-
disappearAction()
242-
})
247+
.onAppear {}
243248
} else {
244249
return configure(image: imageManager.image!)
245250
.id("\(imageModel.url!):\(imagePlayer.currentFrameIndex)")
246-
.onPlatformAppear(appear: {
251+
.onAppear {
247252
if shouldResetPlayer {
248253
// Clear previous status
249254
self.imagePlayer.stopPlaying()
250-
self.imagePlayer.player = nil;
255+
self.imagePlayer.player = nil
251256
self.imagePlayer.currentFrame = nil;
252257
self.imagePlayer.currentFrameIndex = 0;
253258
self.imagePlayer.currentLoopCount = 0;
@@ -262,9 +267,7 @@ public struct WebImage : View {
262267
self.imagePlayer.setupPlayer(animatedImage: animatedImage)
263268
self.imagePlayer.startPlaying()
264269
}
265-
}, disappear: {
266-
disappearAction()
267-
})
270+
}
268271
}
269272
}
270273

0 commit comments

Comments
 (0)