Notable Apps


By Frontier Design Group
An audio toy using the device’s accelerometer to generate theremin-like audio. Rotate the phone to adjust the pitch and vibrato.

Papa Sangre

By Somethin’ Else
An audio-driven game with no graphics.

Air Hockey Gold

By Acceleroto
Air hockey game with single device multiplayer option.

iHandy Carpenter

By iHandySoft
A set of carpentry tools like levels and rulers using the device’s accelerometer and gyroscope.


By Shadowforce
A paintball-like game where the device’s camera is used to detect other players using computer vision.

Noby Noby Boy

By NAMCO Bandai Games
A game where you stretch a character as far as you can by actually traveling in the real world.


By hexler
A modular interface for controlling OSC and MIDI software interfaces.


A large scale video installation where content is displayed, captured, and triggered using iPads.

Scroll Views

Scroll views are used when content that needs to be displayed won’t fit in its entirety on the screen. The UIScrollView class provides functionality to:

  • Scroll through content that is bigger than the size of the view, using pan gestures.
  • Zoom in and out of content, using pinch gestures.


Scrolling is fairly straightforward, no special delegation or gesture recognizers are required. The UIScrollView simply scrolls its subviews.

  1. Add a UIScrollView to the display.
  2. Build some subviews, setting their frame property to position them in the scroll view.
  3. Set the scroll view’s contentSize property to a value great enough to cover all the subviews. contentSize does not get set automatically and defaults to CGSizeZero, so don’t forget this step!
Header Source

Steps 1 and 2 can be performed in the Interface Builder as well, but step 3 must be done in code.

We can retrieve the content offset using the bounds or contentOffset properties.

We can update our ViewController to conform to the UIScrollViewDelegate protocol, and read out the content offset whenever it changes by implementing - (void)scrollViewDidScroll:(UIScrollView *)scrollView.

Header Source


Zooming requires a couple of extra steps.

  1. Set the scroll view’s minimumZoomScale and maximumZoomScale properties.
  2. Register a delegate on the UIScrollView which implements - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView.

That’s all it takes to zoom using the pinch gesture. If you need to support additional gestures (for example, double-tap to zoom all the way out), you will need to add the corresponding UIGestureRecognizer on the scroll view.

Manually Scrolling and Zooming

As a final exercise, let’s programmatically add a couple of UIButtons to the view for saving and restoring the current scroll offset and zoom scale.


A protocol declares methods that can be implemented by any class. It is not a class itself, but it defines a set of methods that objects conforming to it are responsible for implementing. This is the same concept as an interface in Java.

@protocol is similar to @interface, but we don’t need to worry about the implementing. It is also declared in a header file.

Let’s add a protocol for our Animal.

Header Source

A protocol can extend another protocol.

Header Source

A protocol can have optional methods.

Header Source

We can declare variables that conform to a protocol without implementing a specific class.

The main use for protocols in Cocoa is for delegates and data sources.

A delegate is a responder object that acts on behalf of, or in coordination with, another object when that object encounters a user event in a program. The delegate is asked to interpret that event in an application-specific manner.

For example, the ViewController in our FeedTheCat app is a delegate for the “Touch Up Inside” event on the UIButtons in the view.

A data source is like a delegate except that, instead of being delegated control of the user interface, it is delegated control of data.

  1. Protocols. Learning Objective-C: A Primer. Apple, Sep 1 2010.

  2. Delegates and Data Sources. Cocoa Fundamentals Guide. Apple, Dec 13 2010.

Gesture Recognizers

There are two ways to handle touches on a view.

  1. By responding to raw touch events (touch down, touch moved, touch up).
    This is similar to using the mousePressed(), mouseDragged(), mouseReleased() functions in Processing.
    It is also how we’ve been interfacing with UIButton objects.
  2. By registering actions on certain predefined gestures (tap, swipe, pan, pinch, rotation, long press).
    This is done using subclasses of UIGestureRecognizer.

The UIGestureRecognizer Class

UIGestureRecognizer is abstract, meaning that we can’t ever use it directly. We need to use one of its concrete subclasses:

  • UITapGestureRecognizer listens for single or multiple taps, involving one or many touches.
  • UIPinchGestureRecognizer listens for pinching gestures, involving two touches (moving two fingers towards or away from each other).
  • UIRotationGestureRecognizer listens for rotation gestures, involving two touches (moving two fingers opposite from each other in a circular motion).
  • UISwipeGestureRecognizer listens for swiping gestures, involving one or many touches.
  • UIPanGestureRecognizer listens for panning gestures, involving one or many touches.
    The difference with UISwipeGestureRecognizer is that the panning gesture is continuous whereas the swiping gesture is triggered after the fact. For example, you would listen for a swipe to change pages in an eBook, but listen for a pan to move pieces around in a puzzle game.
  • UILongPressGestureRecognizer listens for long press gestures, involving one or many touches. The touch must stay down for a determined period of time and must not move more than the allowable distance.

To use a UIGestureRecognizer, it must be added to the UIView it listens on and it must be registered with a callback method to handle the gesture when it happens. This can be done in the Interface Builder or preferably in code.

In order to properly handle an incoming gesture, you must check the state property of UIGestureRecognizer.

Each concrete gesture recognizer also provides methods and properties to help you handle it properly.

  1. Gesture Recognizers. Event Handling Guide for iOS. Apple, Mar 10 2011.


The UIView Class

The UIView class represents a rectangular area and is responsible for managing the content in that area. This includes drawing and animation, subview layout and management, and event handling.


Before looking at view properties, we need to understand structs.

A struct is a type defined in the C language for a structured record. Basically, it is a set of named attributes grouped into a single object. Think of it like a class that only contains public variables.

A struct definition looks like this:

You can then create a new variable of this type and access its member attributes using the dot notation.

When creating struct variables in Objective-C, we don’t use pointers but reference the objects directly.

View Properties

This is just a float. All coordinates and dimensions in views are of type CGFloat.

A struct made up of two CGFloats: x and y.
You can use the CGPointMake(CGFloat x, CGFloat y) function to create a CGPoint.

A struct made up of two CGFloats: width and height.
You can use the CGSizeMake(CGFloat width, CGFloat height) function to create a CGSize.

A struct made up of a CGPoint for the origin and a CGSize for the size. The origin (0, 0) of a view’s coordinate system is top left.
You can use the CGRectMake(CGFloat x, CGFloat, y, CGFloat width, CGFloat height) function to create a CGRect.

UIView has three properties used to position it in the space.

@property (nonatomic) CGRect bounds
The view’s location and size in its own coordinate system.

@property (nonatomic) CGRect frame
The view’s location and size in its superview’s coordinate system.

@property (nonatomic) CGPoint center
The view’s center point in its superview’s coordinate system. Setting this property changes the values in frame accordingly.

Use frame and center to position the view in the hierarchy. Use bounds inside your view’s implementation.


A view has only one superview (parent view), accessed using the superview property.

A view has zero or more subviews, accessed using the subviews array. The subviews are drawn in the same order as their index in the array (the later ones are drawn on top).

The topmost UIView is the UIWindow. There is usually only one UIWindow in an iOS app.

The view hierarchy is usually constructed graphically in the Interface Builder. However, it can also be done in code.

- (id)initWithFrame:(CGRect)aRect
the designated UIView initializer. Use it with alloc to create a view.

- (void)addSubview:(UIView *)view
adds the passed UIView to the end of the array. It is drawn on top of the other subviews.

- (void)removeFromSuperview
removes the UIView this method is called on from its superview.

  1. struct (C programming language). Wikipedia, Sep 12 2012.
  2. Paul Hegarty. Lecture 4 Slides. iPad and iPhone Application Development. Stanford, Nov 14 2011.