Using the ZStack as a container to distinguish the container disappear vs frame disapppear

This commit is contained in:
DreamPiggy 2022-09-23 20:08:40 +08:00
parent ba9db288f4
commit cd85148fc5
2 changed files with 41 additions and 28 deletions

View File

@ -105,11 +105,21 @@ public final class ImagePlayer : ObservableObject {
currentAnimatedImage = animatedImage currentAnimatedImage = animatedImage
if let imagePlayer = SDAnimatedImagePlayer(provider: animatedImage) { if let imagePlayer = SDAnimatedImagePlayer(provider: animatedImage) {
imagePlayer.animationFrameHandler = { [weak self] (index, frame) in imagePlayer.animationFrameHandler = { [weak self] (index, frame) in
self?.currentFrameIndex = index guard let self = self else {
self?.currentFrame = frame return
}
if (self.isPlaying) {
self.currentFrameIndex = index
self.currentFrame = frame
}
} }
imagePlayer.animationLoopHandler = { [weak self] (loopCount) in imagePlayer.animationLoopHandler = { [weak self] (loopCount) in
self?.currentLoopCount = loopCount guard let self = self else {
return
}
if (self.isPlaying) {
self.currentLoopCount = loopCount
}
} }
// Setup configuration // Setup configuration
if let maxBufferSize = maxBufferSize { if let maxBufferSize = maxBufferSize {

View File

@ -102,8 +102,17 @@ public struct WebImage : View {
} }
public var body: some View { public var body: some View {
return Group { // Container
// Render Logic return ZStack {
// This empty Image is used to receive container's level appear/disappear to start/stop player, reduce CPU usage
Image(platformImage: .empty)
.onAppear {
self.appearAction()
}
.onDisappear {
self.disappearAction()
}
// Render Logic for actual animated image frame or static image
if imageManager.image != nil && imageModel.url == imageManager.currentURL { if imageManager.image != nil && imageModel.url == imageManager.currentURL {
if isAnimating && !imageManager.isIncremental { if isAnimating && !imageManager.isIncremental {
setupPlayer() setupPlayer()
@ -136,8 +145,6 @@ public struct WebImage : View {
} }
}) })
} }
}.onDisappear {
self.disappearAction()
} }
} }
@ -207,18 +214,20 @@ public struct WebImage : View {
} }
} }
/// Animated Image Disappear should stop display link /// Container level to resume animation when appear
func appearAction() {
self.imagePlayer.startPlaying()
}
/// Container level to stop animation when disappear
func disappearAction() { func disappearAction() {
// Only stop the player which is not intermediate status if self.imageConfiguration.pausable {
if !imagePlayer.isWaiting { self.imagePlayer.pausePlaying()
if self.imageConfiguration.pausable { } else {
self.imagePlayer.pausePlaying() self.imagePlayer.stopPlaying()
} else { }
self.imagePlayer.stopPlaying() if self.imageConfiguration.purgeable {
} self.imagePlayer.clearFrameBuffer()
if self.imageConfiguration.purgeable {
self.imagePlayer.clearFrameBuffer()
}
} }
} }
@ -235,19 +244,15 @@ public struct WebImage : View {
// Bind frame index to ID to ensure onDisappear called with sync // Bind frame index to ID to ensure onDisappear called with sync
return configure(image: currentFrame) return configure(image: currentFrame)
.id("\(imageModel.url!):\(imagePlayer.currentFrameIndex)") .id("\(imageModel.url!):\(imagePlayer.currentFrameIndex)")
.onPlatformAppear(appear: { .onAppear {}
self.imagePlayer.startPlaying()
}, disappear: {
disappearAction()
})
} else { } else {
return configure(image: imageManager.image!) return configure(image: imageManager.image!)
.id("\(imageModel.url!):\(imagePlayer.currentFrameIndex)") .id("\(imageModel.url!):\(imagePlayer.currentFrameIndex)")
.onPlatformAppear(appear: { .onAppear {
if shouldResetPlayer { if shouldResetPlayer {
// Clear previous status // Clear previous status
self.imagePlayer.stopPlaying() self.imagePlayer.stopPlaying()
self.imagePlayer.player = nil; self.imagePlayer.player = nil
self.imagePlayer.currentFrame = nil; self.imagePlayer.currentFrame = nil;
self.imagePlayer.currentFrameIndex = 0; self.imagePlayer.currentFrameIndex = 0;
self.imagePlayer.currentLoopCount = 0; self.imagePlayer.currentLoopCount = 0;
@ -262,9 +267,7 @@ public struct WebImage : View {
self.imagePlayer.setupPlayer(animatedImage: animatedImage) self.imagePlayer.setupPlayer(animatedImage: animatedImage)
self.imagePlayer.startPlaying() self.imagePlayer.startPlaying()
} }
}, disappear: { }
disappearAction()
})
} }
} }