Quick Github PSA

by Bob McCune on September 6, 2011

My Core Animation Demos projects was previously available on Github under my personal account, but I have since moved it to my business account. The new location of of the CA demos is:

https://github.com/tapharmonic/Core-Animation-Demos

I haven’t made any major changes to code at this point, however, I did start an ARC branch to house my ongoing ARC changes.

[ Comments on this entry are closed ]

Automagic Factories in Objective-C

by Bob McCune on April 8, 2011

The Factory pattern is a frequently used creational pattern to help abstract the creation of an object from its clients. Although there are a few specializations of this pattern I’ll focus on the most commonly used approach and then look at how we can leverage the Objective-C Runtime to make this solution more robust.

Let’s begin by creating a simple command-line app that creates Animal objects and gets them to speak on command. The various animal objects will all inherit from the abstract Animal class.
Read More

[ Comments on this entry are closed ]

Core Animation Presentation

by Bob McCune on March 13, 2011

This past week I gave a presentation on Core Animation to the Minnesota CocoaHeads user group. Core Animation is a truly amazing framework and is really the magic ingredient in the iOS user experience. Understanding how to effectively use it can allow you to add new levels of realism and interactivity to your apps.

As part of the presentation I prepared a number of examples ranging from image effects to 3D animations (OK, 2 1/2 D animations). If you’re interested in taking a look you can find the demo on github and the slides are available here.

[ Comments on this entry are closed ]

iOS 101: Pattern Images

by Bob McCune on March 8, 2011

A common need in an iOS app is to tile a background image on a UIView. It’s trivial to implement, but not particularly obvious if you’re new to the platform. The solution is to set the view’s backgroundColor property to a UIColor instance created from your pattern image. For instance, the following code will create a pattern image from pattern.png:

This will produce the following effect:

Like most things in Cocoa there is usually an easy solution to the problem you’re trying to solve, but what it is may not be immediately apparent if you’re just starting out. Hope this helps.

[ Comments on this entry are closed ]

Using iOS 4’s IBOutletCollection

by Bob McCune on January 31, 2011

Cocoa has long defined the IBAction and IBOutlet keywords. These keywords provide metadata hints to Interface Builder providing it some understanding of your underlying code so you can graphically wire instance variables and properties and set target-action behaviors. For instance, whenever you need a pointer to NIB-defined object you would create a property defined as:

The inclusion of the IBOutlet keyword in the saveButton’s property definition makes it visible to Interface Builder allowing this connection to be wired graphically as show below:

Interface Builder

Using IBOutlet makes it very easy to define your UI in Interface Builder and simply “wire” the object references to your code. However, it does come with one major drawback in that the relationship from a NIB-defined object to property/ivar is always 1-to-1; there was no way to define a collection of components as a single property in your code. Thankfully, Apple quietly introduced a new keyword in iOS4+ called IBOutletCollection allowing you to do just this. Let’s take a closer look at an example where this can be useful.

IBOutletCollection

Like the other keywords previously mentioned, you’ll find the IBOutletCollection keyword in UINibDeclarations.h, which is part of the UIKit framework. The new keyword is defined as follows:

You’ll notice that IBOutletCollection takes a class name parameter. This allows you specify what are considered valid values for the collection such as UIView or UIButton. If you’d prefer you can change the “type” argument to id to allow for a heterogeneous collection. So how could this be useful?

Putting IBOutletCollection To Work

I’ve often found times where I needed references to certain components, but really only in an aggregate way so I could change state on them as a group. To do this I’d first need to define separate ivar/property combinations for each component, wire them up in Interface Builder, and then manually collect each pointer into an NSArray and store the collection for later use. Although this works, it is rather tedious and does add unnecessary bloat to your code. A better way to handle this type of scenario is to make use of the IBOutletCollection keyword and wire up this relationship in IB.

In the example below I’ve defined two unique ivars/properties to hold references to my on & off buttons and then an NSArray to hold a collection of other buttons. The on/off buttons are used to change the enabled state of the group of “otherButtons”.

I can now easily wire this together in Interface Builder without the need to write any code to collect and store the references to my group of buttons. This is clearly a contrived example, but I hope you can see the benefit of this small addition to iOS 4.

[ Comments on this entry are closed ]

Objective-C for Java Developers

by Bob McCune on August 10, 2010

I wanted to thank everyone who attended my Objective-C for Java Developers presentation at TCJUG tonight. It was odd giving a non-Java talk at TCJUG, but it was a lot of fun and I hope you found it useful. Objective-C is a fairly significant departure from Java but hopefully the presentation highlighted some areas of commonality and helped bridge some of the gaps.

The slides to the presentation can be found on the “Works” tab above.

[ Comments on this entry are closed ]