Today we began our final game project using UIKit. After this all games will use SpriteKit. In a sense this is kind of sad. It’s like bidding farewell to an old friend that you feel comfortable being around. But like they say, good-byes don’t have to be forever. You can always come back to visit and like all great friendships it’s easy to pick up exactly where you left off.
This project we will be creating a game similar in nature to 7 Little Words. The primary challenge we have in this project is to completely build the user interface programmatically rather than through the Storyboard. I have to admit; I really like Storyboard. It gives me a great visual of what I am trying to accomplish but I can also admit that it sometimes acts as a crutch allowing me to just draw things rather than knowing how it all works.
I understand why we are going through this exercise. We need to know that all things can be done in code. There will be times when it is faster and more time efficient to just write the code rather than drawing it in Storyboard then creating outlets and connecting the pieces. It also gives us some exposure to looking at user interface in code so we can understand what is being built.
Rather than completely describing what the first part of this lesson does, I want to focus on something specific in one of the first steps we did. After creating the variables to hold thinks clues, answers, in-progress answers, player scores, and the buttons for the letters; we then created an override for the function loadView(). This allowed us to create a new instance of a view. We also set the background color to white. It is this last statement I wanted to expand upon.
In the example, there was one simple line that said
view.backgroundColor = .white
This line tells the program to set the background color to white. That works just fine but what if you wanted to change the color? How do you know what color words are appropriate? And what if the color created using the color word is not the exact hue you wanted? Red is not necessarily the red you wanted or were expecting.
Swift has something that I think is kind of cool and makes selecting a color a little easier for you to get the one that is perfect for your application. Let’s change that one line of code above to:
view.backgroundColor = Color Literal
Notice that Color Literal is an auto completion choice. Once you select that and hit enter you will notice that in your Xcode editor you now have a small square of white color. You can double-click that color box and a palette of colors will be displayed which you can choose an appropriate color. If you don’t find one you want, you can select Other… and you are given the Apple Color Picker that you can define whatever color you are looking for.
Once you select the color the color box remains in the editor and you can double-click it at any time to select a different color after seeing how things work. This isn’t the most earth shattering tip but I thought it was kind of cool and something that can be useful if like me you want to keep trying color combinations until you find one that fits in with the aesthetic of the app you are building without trying to figure out color words or hex values.
0 Comments
Trackbacks/Pingbacks