Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- contentview.swiftui
- **********************
- import SwiftUI
- struct ContentView: View {
- //create an rgb color random values
- let rTarget = Double.random(in: 0..<1)
- let gTarget = Double.random(in: 0..<1)
- let bTarget = Double.random(in: 0..<1)
- //show the state of show alert (if we need to present it or hide it)
- @State var showAlert=false
- //the user guess numbers
- @State var rGuess:Double
- @State var gGuess:Double
- @State var bGuess:Double
- func computeScore()->Int{
- let rDiff = rGuess - rTarget
- let gDiff = gGuess - gTarget
- let bDiff = bGuess - bTarget
- //sqrt(rdiff^2 + gDiff^2 + bDiff^2)
- let diff = sqrt(rDiff * rDiff + gDiff*gDiff + bDiff*bDiff)
- return Int((1.0-diff)*100+0.5)
- }
- var body: some View {
- VStack{
- HStack{
- //target color block
- VStack{
- Rectangle()
- .foregroundColor(Color(red: rTarget, green: gTarget, blue: bTarget, opacity: 1.0))
- Text("Match this color")
- }
- //guess color block
- VStack{
- Rectangle()
- .foregroundColor(Color(red: rGuess, green: gGuess, blue: bGuess, opacity: 1.0))
- HStack{
- Text("R: \(Int(rGuess*255.0))")
- Text("G: \(Int(gGuess*255.0))")
- Text("B: \(Int(bGuess*255.0))")
- }
- }
- }
- Button(action: {
- //what to do when we hit the button
- self.showAlert = true
- }){
- Text ("Hit me")
- }
- .alert(isPresented: $showAlert){
- Alert(title: Text("Your score"), message: Text("\(computeScore())"))
- }
- VStack{
- ColorSlider(value: $rGuess , textColor: .red)
- ColorSlider(value: $gGuess, textColor: .green)
- ColorSlider(value: $bGuess, textColor: .blue)
- }
- }
- }
- }
- struct ColorSlider : View {
- @Binding var value:Double
- var textColor: Color
- var body: some View{
- HStack{
- Text("0")
- .foregroundColor(textColor)
- Slider(value: $value)
- Text("255")
- .foregroundColor(textColor)
- }
- .padding()
- }
- }
- struct ContentView_Previews: PreviewProvider {
- static var previews: some View {
- ContentView(rGuess: 0.5, gGuess: 0.5, bGuess: 0.5)
- }
- }
- sceneDelegate.swift
- *******************************************
- func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
- // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
- // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
- // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
- // Create the SwiftUI view that provides the window contents.
- let contentView = ContentView(rGuess: 0.5, gGuess: 0.5, bGuess: 0.5)
- // Use a UIHostingController as window root view controller.
- if let windowScene = scene as? UIWindowScene {
- let window = UIWindow(windowScene: windowScene)
- window.rootViewController = UIHostingController(rootView: contentView)
- self.window = window
- window.makeKeyAndVisible()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement