Skip to Content
Swift Development with Cocoa
book

Swift Development with Cocoa

by Paris Buttfield-Addison, Jonathon Manning, Tim Nugent
December 2014
Intermediate to advanced
480 pages
11h 31m
English
O'Reilly Media, Inc.
Content preview from Swift Development with Cocoa

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

iOS Development with Swift

iOS Development with Swift

Craig Grummitt
Core Data in Swift

Core Data in Swift

Marcus S. Zarra

Publisher Resources

ISBN: 9781491909683Errata