Skip to content

Commit 88b0d86

Browse files
committed
Update the demo with dynamic data, Swipe to delete
1 parent 152a321 commit 88b0d86

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

Example/SDWebImageSwiftUIDemo/ContentView.swift

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ extension String : Identifiable {
1717
}
1818
}
1919

20-
let imageURLs = [
20+
struct ContentView: View {
21+
@State var imageURLs = [
2122
"http://assets.sbnation.com/assets/2512203/dogflops.gif",
2223
"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
2324
"http://apng.onevcat.com/assets/elephant.png",
@@ -30,8 +31,6 @@ let imageURLs = [
3031
"https://nokiatech.github.io/heif/content/image_sequences/starfield_animation.heic",
3132
"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
3233
"http://via.placeholder.com/200x200.jpg"]
33-
34-
struct ContentView: View {
3534
@State var animated: Bool = true // You can change between WebImage/AnimatedImage
3635

3736
var body: some View {
@@ -73,30 +72,37 @@ struct ContentView: View {
7372
}
7473

7574
func contentView() -> some View {
76-
List(imageURLs) { url in
77-
NavigationLink(destination: DetailView(url: url, animated: self.animated)) {
78-
HStack {
79-
#if os(iOS) || os(tvOS) || os(macOS)
80-
if self.animated {
81-
AnimatedImage(url: URL(string:url))
82-
.resizable()
83-
.scaledToFit()
84-
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
85-
} else {
75+
List {
76+
ForEach(imageURLs) { url in
77+
NavigationLink(destination: DetailView(url: url, animated: self.animated)) {
78+
HStack {
79+
#if os(iOS) || os(tvOS) || os(macOS)
80+
if self.animated {
81+
AnimatedImage(url: URL(string:url))
82+
.resizable()
83+
.scaledToFit()
84+
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
85+
} else {
86+
WebImage(url: URL(string:url))
87+
.resizable()
88+
.scaledToFit()
89+
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
90+
}
91+
#else
8692
WebImage(url: URL(string:url))
8793
.resizable()
8894
.scaledToFit()
8995
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
96+
#endif
97+
Text((url as NSString).lastPathComponent)
9098
}
91-
#else
92-
WebImage(url: URL(string:url))
93-
.resizable()
94-
.scaledToFit()
95-
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
96-
#endif
97-
Text((url as NSString).lastPathComponent)
9899
}
99100
}
101+
.onDelete(perform: { (indexSet) in
102+
indexSet.forEach { (index) in
103+
self.imageURLs.remove(at: index)
104+
}
105+
})
100106
}
101107
}
102108

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Tips:
129129

130130
1. Use `Switch` (right-click on macOS) to switch between `WebImage` and `AnimatedImage`.
131131
2. Use `Reload` (right-click on macOS/force press on watchOS) to clear cache.
132+
3. Use `Swipe` to delete one image item.
132133

133134
## Screenshot
134135

0 commit comments

Comments
 (0)