You can find the source code as well on my Github page.
In SwiftUI you use "Stacks" to align the content on the screen. In this App I use:
Once the slider is moved, the slider value will be multiplied to each of the different generated color values which causes the color to change anytime the app is started.
var redValue = Double.random(in: 0...0.8)
var greenValue = Double.random(in: 0...0.8)
var blueValue = Double.random(in: 0...0.8)
var randomValue = Double.random(in: 0...0.5)
@State private var sliderValue: Double = 0.6
ZStack {
Color(
red: redValue * sliderValue,
green: greenValue * sliderValue,
blue: blueValue * sliderValue,
opacity: 0.3)
VStack(alignment: .center) {
Image("Hive")
.resizable()
.scaledToFit()
.frame(width: 300, height: 300, alignment: .center)
.foregroundColor(Color(
red: redValue * (sliderValue + randomValue),
green: greenValue * (sliderValue + randomValue),
blue: blueValue * (sliderValue + randomValue),
opacity: 0.8))
}
Slider(value: $sliderValue, in: 0.2...1, step: 0.01)
Text("red: \(255 * (redValue * (sliderValue + randomValue)), specifier: "%.0f")")
Text("green: \(255 * (greenValue * (sliderValue + randomValue)), specifier: "%.0f")")
Text("blue: \(255 * (blueValue * (sliderValue + randomValue)), specifier: "%.0f")")
That is the breakdown of the code used for this app.
Please check my my Github page for the entire code or let me know in the comments section if you have questions.