Peekaboo Ios App #7: Color Palette | Swift 4.2 And Xcode 10

Welcome back to part 7 of our peekaboo series in this episode we’re going to teach our ghosts to be like a chameleon and change colors so let’s do it now I might be asking herself Gabe didn’t we do that last time and in the way we sorta did so last time we wrote line 51 which basically is changing.

The tint color of our peekaboo imageview to be this purplish blue color but this is not really.

Good programming style because we want to keep all the logic of our object when.
They make sense to do so with that object so what we’re going to do.

In this episode is we’re going to use enums and dictionaries and implement a color palette for our peekaboo so what this means is we’re going to say hey peekaboo now like a chameleon you can you can change yourself to however many colors we include in our in our enum so how do we do this well the first step is we go to our peekaboo Swift file we need to import the uikit framework because this is where.

UI colors are inside and then inside the peekaboo class we’ll make some space and we want to first.

Start out by writing an enum to do that you go enum which by the way sent for enumeration press Enter.

The name is going to be color name and typically I don’t know why if you guys know why comment down below but we don’t follow what’s called camel case for enum names see how typically example here line v sorry not 54:24 speech is lower cased and then synthesizer there’s upper.

Case this called camel case for some reason enum names don’t follow that convention like class names if you guys.

Know why hit it down below but in other case we’re going to make the type of this enum to be integer this would be clear in next episode wink-wink stay tuned for that but here’s where we want to put the color names so let’s start simple let’s just go good old RGB so case red case green man I can’t think today case blue so now we have our enum right but that.
Is not really a color so how do we.

Get the actual now colors well we’re going to create a static variable called colors and this would be our dictionary whose keys are the enum and whose values are a UI color now we want to actually define the keys and values we go dot red we.

Use our good old color literal it sorry guys not a comma what am i doing semicolon you want.
To use our color literal here let’s choose a nice red.

Let’s call this one here maraschino cannot pronounce that now you want to go dot green again color literal let’s choose a nice green not too bright like last time let’s go something a little bit darker I like the sauce color.

So Moss and now our last color is good old blues a lot blue you know the drill color literal double click now let’s choose aqua that’s good blue right there so now let’s build the project compile should be happy all right both succeeded now.

Here’s an interesting thing we can go back to our peekaboo view controller back to good ol line 51 and now all we can do is we can go peekaboo.

Cell colors right dictionary so access it with the square brackets and now we just write the color name so let’s go dot read if we run the project fingers crossed our peak of the image view should be now red so let’s see project launching huh all right sweet so there it works and then we can try the other.

Color so we can try green and this is going to work hopefully so.

It’s loading and beautiful we have green.

What I really like about this method even though it’s a little bit more complex because you have to use a static dictionary and then you have to use an enum for the keys of the dictionary it’s a lot more expressive remember how I mentioned in the earlier part of this series where you really want to write code that is going to.

Be readable by humans so this.

Makes a lot more sense right because you can read peekaboo image view dot tint color so a tint color of peekaboo image view is being assigned a value of peekaboo dark colors dark green and so you can imagine if your peekaboo had a hundred colors you had light red dark red wine move just by looking at it as if you just had.

Like before having this color literal in your code it might be hard to distinguish between the the subtleties of different colors but now because we’re using a named example in this.

Case green we’re able to really quickly know the actual color so I think this is a great way of doing this one more thing I’d like to show you guys is if you noticed we have here we have basically specifically line 22 we have a static variable which is accessed by using the name of the class has peekaboo dot colors don’t be confused because this is lowercase peekaboo which this means the class lowercase peekaboo.

Is the object so we didn’t actually have to instantiate a peekaboo object or ease the one we already have instantiated which is this one to access the colors I just like to like doing it this way it just makes sense.

Can do is our peekaboo model is currently kind of messy right because we have our instance methods and then we have static method slash really more like properties so to clean this.

Up and let’s see if this works finger crossed this never does guys does it s create a mark here let’s go um static let’s call it methods means not really a method but so to do this is we want to create an extension of our our peekaboo object and we’re going to.

Cut these properties and include them here now we can clean up this code a little bit and if all works which it never does this should be the exact same as before so let’s build a project and let’s see what happens they’ll succeeded that’s a great sign let’s run.

Our project and let’s see it’s loading trigger so just like before always well I really liked using this way of putting extensions because now if you have it let’s say working on a large team of I don’t.

Know say ten people and you were in charge of Critias peekaboo class and you’re on vacation or something and now your teammate has come in and change it let’s.

Say I don’t know fix something.

Well now it’s obvious right because you further separated your code into what part of peekaboo is part of the instance of the object which is this upper part here as in which parts are accessible when you create an object but down below here which we just.

Did is these parts these lines of codes are only accessed by using not the object not an instance so I personally like doing it this way I really like extensions for grouping separating fourth I guess further separating logical pieces of.

Code within the same file right typically have an object or another different grouping of logic in your file well that’s all I have for you guys.

Thank you so much for watching subscribe like the video comment down below much much love consider subscribing until next time.

Posted in Programming<a href="https://baby2471.com/tag/app-development" rel="tag">app development</a> <a href="https://baby2471.com/tag/app-tutorial" rel="tag">app tutorial</a> <a href="https://baby2471.com/tag/app-video-tutorial" rel="tag">app video tutorial</a> <a href="https://baby2471.com/tag/coding-tutorial" rel="tag">coding tutorial</a> <a href="https://baby2471.com/tag/coding-video-tutorial" rel="tag">coding video tutorial</a> <a href="https://baby2471.com/tag/dictionary" rel="tag">dictionary</a> <a href="https://baby2471.com/tag/enum" rel="tag">enum</a> <a href="https://baby2471.com/tag/instantiate" rel="tag">instantiate</a> <a href="https://baby2471.com/tag/instantiate-inc" rel="tag">instantiate inc</a> <a href="https://baby2471.com/tag/ios" rel="tag">ios</a> <a href="https://baby2471.com/tag/ios-app" rel="tag">ios app</a> <a href="https://baby2471.com/tag/ios-apps" rel="tag">ios apps</a> <a href="https://baby2471.com/tag/ios-design" rel="tag">ios design</a> <a href="https://baby2471.com/tag/ios-dev" rel="tag">ios dev</a> <a href="https://baby2471.com/tag/ios-development" rel="tag">ios development</a> <a href="https://baby2471.com/tag/learn-programming" rel="tag">learn programming</a> <a href="https://baby2471.com/tag/learn-to-code" rel="tag">learn to code</a> <a href="https://baby2471.com/tag/learn-to-program" rel="tag">learn to program</a> <a href="https://baby2471.com/tag/peekaboo" rel="tag">peekaboo</a> <a href="https://baby2471.com/tag/programming" rel="tag">programming</a> <a href="https://baby2471.com/tag/programming-tutorial" rel="tag">programming tutorial</a> <a href="https://baby2471.com/tag/software" rel="tag">software</a> <a href="https://baby2471.com/tag/software-development" rel="tag">software development</a> <a href="https://baby2471.com/tag/software-engineering" rel="tag">Software Engineering</a> <a href="https://baby2471.com/tag/software-tutorial" rel="tag">software tutorial</a> <a href="https://baby2471.com/tag/software-video-tutorial" rel="tag">software video tutorial</a> <a href="https://baby2471.com/tag/swift" rel="tag">swift</a> <a href="https://baby2471.com/tag/swift-language" rel="tag">swift language</a> <a href="https://baby2471.com/tag/tutorial" rel="tag">Tutorial</a> <a href="https://baby2471.com/tag/uibutton" rel="tag">uibutton</a> <a href="https://baby2471.com/tag/uikit" rel="tag">uikit</a> <a href="https://baby2471.com/tag/uilabel" rel="tag">uilabel</a> <a href="https://baby2471.com/tag/uiview" rel="tag">uiview</a> <a href="https://baby2471.com/tag/uiviewcontroller" rel="tag">uiviewcontroller</a> <a href="https://baby2471.com/tag/xcode" rel="tag">xcode</a>