Lecture #4: Views

Please note, this blog entry is from a previous course. You might want to check out the current one.

The forth lecture is called “4. Views (October 6, 2011)” and can be found via iTunes. Its slides are available directly at Stanford.

It starts with an half-an-hour demo/walkthrough preparing the calculator for the second assignment using functionality learned in the previous lectures. When you have finished this part you should have something similar to like our example on github. Alternatively a sample code can be downloaded from Stanford but it does not include all the functionality from the previous assignments.

The second half of this lecture addresses views and drawing in them. A view has one super view and can have multiple sub views. The coordinate system of views uses CGPoints which holds two CGFloats for as x and y value. Its origin is in the upper left corner. The CGSize of a view is defined by two CGFloats width and height. The bounds of a view – the internal drawing space of a view – are definde by CGRect containing a CGPoint as origin and its CGSize. Another CGPoint defines the center of the view in its super views coordinate space. The CGRect frame defines a rectangle in the super views coordinate space which entirely holds the bounds of the view.

Views are created using storyboards in Xcode or via code using alloc and initWithFrame. To draw inside a view you can override drawRect. However, never call drawRect directly. Instead use setNeedsDisplay, or setNeedsDisplayInRect. The actual drawing is done via the Core Graphics framework (which has an C API) by creating a context via paths.

Again, please study the slides before watching the video for maximum experience.


Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *