I have a simple SwiftUI application that regenerates random background on every tap gesture and adds a quote to it.
AsyncImage(url: URL(string: backgroundImgUrl)) { phase in
switch phase {
case .empty:
Color.purple.opacity(0.1)
case .success(let image):
image
.ignoresSafeArea()
.scaledToFit()
.onAppear {
//code here happen for 1 time only even for every background change
}
//can I execute code here??? syntax error happens
case .failure(_):
Image(systemName: "exclamationmark.icloud")
.resizable()
.scaledToFit()
@unknown default:
Image(systemName: "exclamationmark.icloud")
}
}
The problem when I try to add quotes with a random on-tap gesture it appears before the background gets updated. so I want to execute code when every new random background is generated but I couldn't. can you give me some hints, please?
onTapGesture code:
.onTapGesture {
backgroundImgUrl = "https://source.unsplash.com/1600x900/?nature&\(Int.random(in: 1..<6236))"
}