可启用代码:
import SwiftUI
class Foo {
var title: String
var url: String
var image: Image?
init(title: String, url: String, image: Image? = nil) {
self.title = title
self.url = url
self.image = image
}
}
struct ContentViewA: View {
@State var showSheetA: Bool = false
var body: some View {
VStack {
Text("This is main view")
}
.onAppear{
showSheetA = true
}
.sheet(isPresented: $showSheetA) {
SheetViewA()
}
}
}
struct SheetViewA: View {
@State var data = [
Foo(title: "Image E", url: "https://t3.ftcdn.net/jpg/10/08/34/50/360_F_1008345045_VOe4ziz83vb6d3E3X6KI00qHyLd32D4l.jpg123", image: nil)
]
@State var panelDetent: PresentationDetent = .medium
var body: some View {
NavigationStack {
VStack {
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .center, spacing: 10) {
ForEach(data, id: \.title) { item in
if let urlObject = URL(string: item.url) {
AsyncImage(url: urlObject,
scale: 1.0,
transaction: Transaction(animation: .spring(response: 0.5, dampingFraction: 0.65, blendDuration: 0.025)),
content: { renderPhoto(phase: $0, item: item) })
} else {
/// Note: Shows placeholder view
EmptyView()
}
}
}
.background(Color.gray.opacity(0.2))
.padding(.leading, 0)
.padding(.trailing, 16)
.frame(maxWidth: .infinity, minHeight: 65, maxHeight: 65, alignment: .topLeading)
}
}
.padding([.top, .bottom], 150.0)
.presentationDetents([.medium, .large], selection: $panelDetent)
}
}
@ViewBuilder
private func renderPhoto(phase: AsyncImagePhase, item: Foo) -> some View {
switch phase {
case .success(let image):
let _ = print("Success state")
case .failure(let error):
let _ = print("%%% detailed error is - \(error.localizedDescription) %%%%")
case .empty:
let _ = print("Empty state")
@unknown default:
EmptyView()
}
}
}
我是我的测试代码,它打印了error
和error.localizedDescription
LoadingError
。
包括测试是使用URLSession