___|  _ \   |  |    |   |_ _|\ \     / ____|
 |     |   |  |  |    |   |  |  \ \   /  __|
 |   | |   | ___ __|  ___ |  |   \ \ /   |
\____|\___/     _|   _|  _|___|   \_/   _____| 

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

1# SwiftUI Tutorial - Change color app

BY: @tarekadam | CREATED: April 6, 2020, 10:56 a.m. | VOTES: 18 | PAYOUT: $1.85 | [ VOTE ]

In this SwiftUI tutorial I will show you how to build an App to change a Logo / Background color via Slider.

[IMAGE: https://files.peakd.com/file/peakd-hive/tarekadam/5WZiBp6I-hiveColor.gif]
You can find the source code as well on my Github page.

Requirements

[IMAGE: https://files.peakd.com/file/peakd-hive/tarekadam/vJexfJww-Scr1.png]

Layout

In SwiftUI you use "Stacks" to align the content on the screen. In this App I use:
- ZStack for showing a background Color
- VStack to display the Image, Slider and Text Vertically to each other.

[IMAGE: https://files.peakd.com/file/peakd-hive/tarekadam/Q8uKSFtI-Scr2.png]

Functionality Overview

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.

Code

    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.

TAGS: [ #tutorial ] [ #swiftui ] [ #programming ] [ #learning ]

Replies

NO REPLIES FOUND.

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>