IMD 1_Xcode RGB Slider 3

The third class of making RGB Slider.

We’ve learned how to update color without clicking.

When we simulate the RGB Slider, the color patch screen is white. But it should be gray because R=127, G=127, B=127. It’s little weird. So we will going to add some code to solve that problem.

  // call the updateColor fuction to set the initial color.

        updateColor(redSlider)

This code will fix that problem.

스크린샷 2016-03-29 오전 10.29.29      스크린샷 2016-03-29 오전 10.29.07

Now our initial color is fixed to grey!

Next step is making a reset button. Search a button at the filter, and drag it to your screen.

스크린샷 2016-03-29 오전 10.53.53.png스크린샷 2016-03-29 오전 10.37.47.png

And connect the line with reset. Write the code like this.

@IBAction func resetPressed(sender: AnyObject) {

        redSlider.value = 0.5

        greenSlider.value = 0.5

        blueSlider.value = 0.5

        updateColor(redSlider)

Now we got our reset button. You can make your own personal preset by changing the number 0.5.

스크린샷 2016-03-29 오후 3.20.24.png      스크린샷 2016-03-29 오후 3.32.37.png

Now we’re going to make an app icon.

The size of the icon is usually 1024*1024 pixels.

스크린샷 2016-03-29 오전 11.11.04.png

First found an image you like, and transform the size in photoshop.

Then go to

http://www.appicon.build

You can get an app icon from there.

스크린샷 2016-03-29 오전 11.15.22.png

Then you’ll have this folder.

스크린샷 2016-03-29 오전 11.16.00.png

Go to Assets.xcassets, and drag the Appicon.appiconset folder.

스크린샷 2016-03-29 오전 11.17.18.png

Now you got an app icon! Easy Peasy.

스크린샷 2016-03-29 오전 11.40.33.png

The code :

//

//  ViewController.swift

//  RGBslider1212200

//

//  Created by 서예정 on 2016. 3. 15..

//  Copyright © 2016 YeJung. All rights reserved.

//

//import the UIKit library

import UIKit

//create a class for the ViewController

class ViewController: UIViewController {

    

    

    //create variables to hold slider values

    

    @IBOutlet weak var redSlider: UISlider!

    @IBOutlet weak var greenSlider: UISlider!

    @IBOutlet weak var blueSlider: UISlider!

    

    

    //create variables for the text labels

    @IBOutlet weak var redLabel: UILabel!

    @IBOutlet weak var greenLabel: UILabel!

    @IBOutlet weak var blueLabel: UILabel!

   

    

    //create a variable for the colorPatch

    

    @IBOutlet weak var colorPatch: UIView!

        

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        // call the updateColor fuction to set the initial color.

        updateColor(redSlider)

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    //create a function that update the color when the slider is moved

    @IBAction func updateColor(sender: AnyObject) {

   

        //create variables to hold the slide values

        let redColor = CGFloat(redSlider.value)

        let greenColor = CGFloat(greenSlider.value)

        let blueColor = CGFloat(blueSlider.value)

        

        //map the slider values from 0 -> 1 to 0 -> 255

        let redMapped = Int(redColor*255)

        let greenMapped = Int(greenColor*255)

        let blueMapped = Int(blueColor*255)

        

        //send the value to the text field

        redLabel.text = String(\(redMapped)”)

        greenLabel.text = String(\(greenMapped)”)

        blueLabel.text = String(\(blueMapped)”)

                

        //set the background color

        //view.backgroundColor = UIColor(red: redColor, green: greenColor, blue: blueColor, alpha: 1)

        

        colorPatch.backgroundColor = UIColor(red: redColor, green: greenColor, blue: blueColor, alpha: 1)

    }

    

    @IBAction func resetPressed(sender: AnyObject) {

        redSlider.value = 0.5

        greenSlider.value = 0.5

        blueSlider.value = 0.5

        updateColor(redSlider)

        

    }

    

    //end of class

}

The Dropbox Link :

https://www.dropbox.com/s/m39txvcgzby5z31/0329RGBSliderhw_1212200.zip?dl=0

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s