[ad_1]
I’ve an inventory for my part
@State var capturedColors: [ColorViewModel] = []
I take advantage of ForEach to create a subview primarily based on the record
ForEach(capturedColors) { shade in
RoundedRectangle(cornerRadius: 10.0).fill(Coloration(shade.cgColor)) //...modifiers ommited
}
A brand new merchandise will probably be appended into the record when a button is tapped.
The view will get up to date accurately when the app runs on a bodily machine, nevertheless it doesn’t present something within the preview.
I did one thing like this within the preview:
struct CaptureView_Previews: PreviewProvider {
static var previewColors: [ColorViewModel] = [
ColorViewModel(CGColor.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)),
]
static var previews: some View {
CaptureView(previewColors) // an initializer assign the parameter to the property
}
}
The ColorViewModel is only a class wrap some shade information:
class ColorViewModel: ObservableObject, Identifiable {
@Revealed var cgColor: CGColor = CGColor(crimson: 1.0, inexperienced: 1.0, blue: 0.5, alpha: 1.0)
init() {}
init(r: UInt8, g: UInt8, b: UInt8) {
cgColor = CGColor(crimson: CGFloat(r), inexperienced: CGFloat(g), blue: CGFloat(b), alpha: 1.0)
}
init(_ cgColor: CGColor) {
self.cgColor = cgColor
}
// ...
}
Methods to make the preview work?
Any assist could be appreciated.
[ad_2]
