Skip to content

Commit 16211f6

Browse files
committed
Supports to sync status for multiple SDAnimatedImageView who use the same image model
1 parent b7af5e6 commit 16211f6

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

Example/SDWebImageSwiftUI.xcodeproj/project.pbxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 52;
6+
objectVersion = 54;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -194,6 +194,7 @@
194194
32E529512348A0DF00EA46FF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
195195
32E529542348A0DF00EA46FF /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
196196
32E529562348A0DF00EA46FF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
197+
32F4114B2BF72FE800B4ECB7 /* SDWebImage */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = SDWebImage; path = ../../SDWebImage; sourceTree = "<group>"; };
197198
3E9F8B5F06960FFFBD1A5F99 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
198199
54859B427E0A79E823713963 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
199200
/* End PBXFileReference section */
@@ -432,6 +433,7 @@
432433
607FACC71AFB9204008FA782 = {
433434
isa = PBXGroup;
434435
children = (
436+
32F4114B2BF72FE800B4ECB7 /* SDWebImage */,
435437
3294617E2AA36761009E391B /* SDWebImageSwiftUI */,
436438
607FACF51AFB993E008FA782 /* Podspec Metadata */,
437439
320CDC2A22FADB44007CF858 /* SDWebImageSwiftUIDemo */,

Example/SDWebImageSwiftUIDemo/ContentView.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ class UserSettings: ObservableObject {
1717
#endif
1818
}
1919

20+
struct ContentView: View {
21+
let imageURL = URL(string: "http://apng.onevcat.com/assets/elephant.png")!
22+
23+
var body: some View {
24+
ScrollView {
25+
LazyVStack {
26+
ForEach(1 ... 200, id: \.self) { index in
27+
HStack {
28+
AnimatedImage(url: imageURL)
29+
.customLoopCount(0)
30+
.playbackMode(.normal)
31+
.resizable()
32+
.frame(width: 100, height: 100)
33+
.environment(\.animationGroup, "A")
34+
AnimatedImage(url: imageURL)
35+
.customLoopCount(0)
36+
.playbackMode(.reverse)
37+
.resizable()
38+
.frame(width: 100, height: 100)
39+
.environment(\.animationGroup, "B")
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
2047
// Test Switching nil url
2148
struct ContentView3: View {
2249
@State var isOn = false
@@ -107,7 +134,7 @@ struct ContentView2: View {
107134
}
108135
}
109136

110-
struct ContentView: View {
137+
struct ContentView4: View {
111138
@State var imageURLs = [
112139
"http://assets.sbnation.com/assets/2512203/dogflops.gif",
113140
"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ public struct AnimatedImage : PlatformViewRepresentable {
280280

281281
func makeView(context: Context) -> AnimatedImageViewWrapper {
282282
let view = AnimatedImageViewWrapper()
283+
if let group = context.environment.animationGroup {
284+
view.wrapped.animationGroup = group
285+
}
283286
if let viewCreateBlock = imageHandler.viewCreateBlock {
284287
viewCreateBlock(view.wrapped, context)
285288
}
@@ -347,6 +350,9 @@ public struct AnimatedImage : PlatformViewRepresentable {
347350
}
348351

349352
func updateView(_ view: AnimatedImageViewWrapper, context: Context) {
353+
if let group = context.environment.animationGroup {
354+
view.wrapped.animationGroup = group
355+
}
350356
// Refresh image, imageModel is the Source of Truth, switch the type
351357
// Although we have Source of Truth, we can check the previous value, to avoid re-generate SDAnimatedImage, which is performance-cost.
352358
let kind = imageModel.kind
@@ -384,11 +390,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
384390

385391
static func dismantleView(_ view: AnimatedImageViewWrapper, coordinator: Coordinator) {
386392
view.wrapped.sd_cancelCurrentImageLoad()
387-
#if os(macOS)
388-
view.wrapped.animates = false
389-
#else
390-
view.wrapped.stopAnimating()
391-
#endif
392393
if let viewDestroyBlock = viewDestroyBlock {
393394
viewDestroyBlock(view.wrapped, coordinator)
394395
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) DreamPiggy <[email protected]>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
import SwiftUI
10+
11+
public struct AnimationGroup: EnvironmentKey {
12+
public static var defaultValue: String? { nil }
13+
}
14+
15+
extension EnvironmentValues {
16+
public var animationGroup: String? {
17+
get { self[AnimationGroup.self] }
18+
set { self[AnimationGroup.self] = newValue }
19+
}
20+
}

0 commit comments

Comments
 (0)