O'Reilly logo

Swift Development with Cocoa by Tim Nugent, Jonathon Manning, Paris Buttfield-Addison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. Drawing Graphics in Views

The fundamental class for showing any kind of graphical image to the user is the view. Graphical images are things like buttons, photos, and text—anything that the user can see.

Cocoa and UIKit provide a wide variety of controls that suit almost all needs—you can display text, images, buttons, and more. However, some data needs to be drawn in a specific way: you might want to draw a chart for data, or create a custom button class that displays exactly the way you want it to. If you’re making a graphics app, you’ll need to be able to display any kind of graphical content, which means that your code will need to know how to draw it.

In this chapter, you’ll learn how to create custom view objects that display any kind of image to the user. You’ll learn how to use the high-level APIs for drawing, and create a custom view class that will scale up to any size at all without losing quality. Finally, you’ll learn how to design your graphics to take advantage of the Retina display on iOS and OS X hardware.

How Drawing Works

Before we start writing code that draws content for the user to see, it’s helpful to review how graphics work in OS X and iOS. Note that the same terminology and techniques apply for both OS X and iOS, but the specific API is different.

When an application draws graphics, it does so by first creating a canvas for drawing in. Cocoa calls this the graphics context. The context defines, among other things, the size of the canvas and how color ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required