Swift – Camera and UIImagePickerController

Take pictures with the UIImagePickerController. This is a ViewController subclass, that creates a view that allows you to take a picture with the camera.

The UIImagePickerController works with a Delegate to handle working with an image after it has been taken.

Try it for yourself. Create a ViewController with a UIImageView, add an IBOutlet for this. We will use this to display the image, so we need a reference.

Add a UIButton, and add an IBAction. Use this to open the Image Picker controller.

In the button action add:

 

Swift – Enum

Organizing Code with Enums

Often when programming you keep track of fixed sets of things. These could be choices like how you will view something, for example Year, Month, Week, or Day. Other sets might be related to the capabilities of your app like the orientation: Portrait, Landscape. Or it could be a list enemy or character types a game might create. Another example might be the colors of tags that an app might use to organize elements.

When creating these things you look for ways to organize them. Often they end up in lists or arrays. While this works well there are some limits and awkward situations that arise. For example if you created a list of colors:

tagColors = [Red, Orange, Yellow, Green, Blue]

You would need to access these colors via their index. Which is awkward. For example:

tagColors[0] // Red

There is also a problem in that your tag colors might be fixed, the Array provides many methods that allow the list to be modified. While you can work around many of these things, there is also ready made construct for this purpose the Enum.

The Enum

Enum or Enumeration is similar to a Struct though more limited. Enums provide a system for creating variables that have very specifically defined values. Use an enum when you want to ensure that a predefined set of values are used.

For example, imagine your program wants to set the colors of some tags. The colors are predefined as: Red, Orange, Yellow Green, Blue, and Purple, an Enum provides a perfect way to keep these organized, while also making your code more organized and easier to work with.

Named Values and Enums

Names like Red, and Green work well in code. Writing the actual color in code is difficult to work with.

  • #b32f3c – Red
  • #f8941d – Orange
  • #e7d936 – Yellow
  • #39b54a – Green
  • #0072bc – Blue
  • #92278f – Purple

Just looking at this it’s really hard to tell Yellow: #e7d936 from Green: #39b54a.

Working with these colors in Xcode there is the added problem that you will most often want a UIColor() which requires more than knowing the hex value. So the colors above might actually look like this in code:

  • UIColor(red: 179, green: 47, blue: 60, alpha: 1) – Red
  • UIColor(red: 248, green: 148, blue: 29, alpha: 1) – Orange
  • UIColor(red: 231, green: 217, blue: 54, alpha: 1) – Yellow
  • UIColor(red: 57, green: 181, blue: 74, alpha: 1) – Green
  • UIColor(red: 0, green: 114, blue: 188, alpha: 1) – Blue
  • UIColor(red: 146, green: 39, blue: 143, alpha: 1) – Purple

To make make a UIColor out of Red we need to write something like:

UIColor(red: 179, green: 47, blue: 60, alpha: 1)

That’s a lot to write. If it were used more than once in our program, and it probably will be, there will be a lot of code for us to write, which translates into more chances for errors, and difficulty in making changes.

You might also want the Hex value: #b32f3c, at another point, and the color name: Red at another time. You might want to display a table cell, set the background to the color, with UIColor(), you may also want to display the tag name: Red on the right side. Both of these items are tied together and related. To work with them in code we want the same type of relationship. We want to talk about Red, and have that mean the UIColor(), and the name, and anything else associated with Red. An Enum is a programming structure in Swift that is made for exactly this.

enum TagColor {
    case Red, Orange, Yellow, Green, Blue, Purple
}

This is an Enum named: TagColor, it defines 6 named values. You can put each on it’s own line also:

enum TagColor {
    case Red
    case Orange
    case Yellow
    case Green
    case Blue
    case Purple
}

Any variable can now hold a reference to one of the colors.

var myColor = TagColor.Red

You can Type something as TagColor

var aColor: TagColor

Now you have to assign this item a TagColor.

aColor = TagColor.Blue

If something is typed to an Enum you can shorten the value to a dot and the name:

aColor = .Blue

Methods in an Enum

An Enum can contain methods. Using a method your Enum can return a UIColor object for each color. Use a switch statement that returns a UIColor for each of the enum keywords  we defined. This switch statement must provide a case for all of the key words. For example add function called toUIColor, that returns a UIColor:

func toUIColor() -> UIColor {
switch self {
    case .Red:
        return UIColor(red: 179, green: 47, blue: 60, alpha: 1)

    case .Orange:
        return UIColor(red: 248, green: 148, blue: 29, alpha: 1)

    case .Yellow:
        return UIColor(red: 231, green: 217, blue: 54, alpha: 1)

    case .Green:
        return UIColor(red: 57, green: 181, blue: 74, alpha: 1)

    case .Blue:
        return UIColor(red: 0, green: 114, blue: 188, alpha: 1)

    case .Purple:
        return UIColor(red: 146, green: 39, blue: 143, alpha: 1)

    }

}

Call a method in an Enum like this:

TagColor.Blue.toUIColor()

or

var aColor = TagColor.Orange.toString() // Returns String UIColor

Here are all of the things above in a playground.

import UIKit

enum Suit {
    case Spades, Hearts, Diamonds, Clubs
}

enum Colors {
    case Red, Yellow, Green, Cyan, Blue, Magenta
    
    func toHex() -> String {
        switch self {
        case .Red:
            return "#FF0000"
        case .Yellow:
            return "#FFFF00"
        case Green:
            return "#00FF00"
        case .Cyan:
            return "#00FFFF"
        case .Blue:
            return "#0000FF"
        case .Magenta:
            return "#FF00FF"
        }
    }
}

var red = Colors.Red.toHex()
println("Color:\(Colors.Red.toHex())")

enum TagColor {
    case Red, Orange, Yellow, Green, Blue, Purple
    
    func toUIColor() -> UIColor {
        switch self {
        case .Red:
            return UIColor(red: 179, green: 47, blue: 60, alpha: 1)
        case .Orange:
            return UIColor(red: 248, green: 148, blue: 29, alpha: 1)
        case .Yellow:
            return UIColor(red: 231, green: 217, blue: 54, alpha: 1)
        case .Green:
            return UIColor(red: 57, green: 181, blue: 74, alpha: 1)
        case .Blue:
            return UIColor(red: 0, green: 114, blue: 188, alpha: 1)
        case .Purple:
            return UIColor(red: 146, green: 39, blue: 143, alpha: 1)
        }
    }
    
    func toHex() -> String {
        switch self {
        case .Red:
            return "#b32f3c"
        case .Orange:
            return "#f8941d"
        case .Yellow:
            return "#e7d936"
        case .Green:
            return "#39b54a"
        case .Blue:
            return "#0072bc"
        case .Purple:
            return "#92278f"
        }
    }
    
    func toString() -> String {
        switch self {
        case .Red:
            return "Red"
        case .Orange:
            return "Orange"
        case .Yellow:
            return "Yellow"
        case .Green:
            return "Green"
        case .Blue:
            return "Blue"
        case .Purple:
            return "Purple"
        }
    }
}

var myTag = TagColor.Red
var yourTag = TagColor.Green
var aColor: TagColor

aColor = .Blue

TagColor.Blue.toString()

println(myTag.toHex())
println(myTag.toUIColor())
println(myTag.toString())

// A function that takes an enum value
func makeTag(color: TagColor) {
    println("makes a tag of color:\(color.toString())")
}

makeTag(TagColor.Purple)

// If the type is an enum you can shorten to a dot and the keyword name
makeTag(.Green)

Touch screen interaction design challenge

Back in the olden days, we’re talking the 90s, there was shockwave. This was before Flash. Shockwave was the web component of Director.

At at the time there was a site Playcom if I recall, that had many Shock wave games some of which were pretty good. One of the games I thought was well conceived, though difficult to play, was called Tune Inn. The concept was a record store where you made records for customers. Customers wore shirts in a color  and the idea was to make records in colors that matched. You played two characters Pekka and Moniq. Pekka helped customers at the counter while Moniq made records upstairs. Heres a picture.

image

The original game used the keyboard to control and switch between characters. Here’s the challenge, how would you adapt this to a touch screen game?

Corona – Sprites Simple Game

Here’s an example that builds off the last post about creating an explosion sprite. In this example we’ll create some alien sprites that move down the screen. When touched these aliens explode.

The explosion sprite sheet and code was taken from this post: Corona – Sprite Explosion.

For this example I used this sprite sheet for the alien: 

Continue reading Corona – Sprites Simple Game