A block is simply a block of code that can be used as a method argument. You can create variables to hold blocks (which we are not going to cover today), or you can add blocks in-line with the calling method.

Blocks are useful for defining method callbacks (for example, handling the response of an HTTP request) and completion handlers (for example, waiting for an animation to complete before moving on). The advantage is that all the code pertaining to a particular operation is grouped together instead of spread out all over a class implementation.

A block starts with a caret ^, optionally followed by arguments in parentheses (...), followed by code wrapped in curly brackets {...}.

Local variables can be used inside blocks, but they are read-only.

If you want to set local variables, you need to mark them with the __block keyword.

Instance variables can be used inside blocks without any special flags.

  1. Paul Hegarty. Lecture 10 Slides. iPad and iPhone Application Development. Stanford, Nov 24 2011.


NSTimer is used to perform operations after a specified delay. An NSTimer can also be set to repeat, to perform a set of operations over and over again after a specified delay.

The easiest way to create an NSTimer is using the class method scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:.

To create a repeating timer, set the repeats parameter to YES.

To stop an NSTimer, call its invalidate method.

If you want to pass parameters to the timer callback, you can store them in the userInfo dictionary.

As an alternative, you can use NSObject‘s performSelector:withObject:afterDelay: method.

Smiley App

Build an app with a custom UIView that draws a smiley face.

  • You can only use Core Graphics methods for drawing.
  • The face must scale properly depending on the size of the view.
  • The face does not need to be identical to the example, but you should use this exercise to explore the many different options offered by the Core Graphics framework.

If you finish before the end of class, add some animation to your smiley face.

Here is my version.



Custom Drawing

You will sometimes need to do some of your own drawing, for example when building a custom button shape. To do this, you just need to subclass UIView and override the - (void)drawRect:(CGRect)aRect; method.

You should never call drawRect: directly. Instead, you call - (void)setNeedsDisplay;, which tells your view to call drawRect: when it’s ready to do so.

Core Graphics Framework

Drawing is achieved using the Core Graphics framework (also sometimes referred to as Quartz 2D). Core Graphics is written in pure C, meaning that it’s not object-oriented and that it doesn’t work with Objective-C syntax (no brackets).

First, you get a context to draw into. The context determines where your drawing goes. This is usually the screen, but can also be a PDF, a printer, etc.

Then, you create paths using lines, arcs, etc. This is similar to drawing shapes in Processing, Flash, and everything else; you just have a sequence of move to, line to, arc to statements.

Next, you set colors, line widths, line caps, etc.

Finally, you render the paths created above.

Here is that same code in a UIView subclass.

Instead of drawing lines all around, you can simply close the shape when you’re done.

You can also build a shape using a CGRect. Note that if you want to render the fill and stroke, you must do so in two steps.

To render circles and ellipses, you must use a CGRect.

It is sometimes easier to use the - (void)setFill and - (void)setStroke methods in UIColor to set your colours.

There are many more functions available to customize your drawing. You can find them all in the CGContext Reference.

  1. Quartz 2D Programming Guide. Apple, Visited Sep 24 2012.