Book Image

Swift Essentials - Second Edition

By : Alex Blewitt
Book Image

Swift Essentials - Second Edition

By: Alex Blewitt

Overview of this book

Swift was considered one of the biggest innovations last year, and certainly with Swift 2 announced at WWDC in 2015, this segment of the developer space will continue to be hot and dominating. This is a fast-paced guide to provide an overview of Swift programming and then walks you through in detail how to write iOS applications. Progress through chapters on custom views, networking, parsing and build a complete application as a Git repository, all by using Swift as the core language
Table of Contents (17 chapters)
Swift Essentials Second Edition
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Custom graphics with drawRect


Subclasses of UIView can implement their own custom graphics by providing a drawRect method that implements the custom drawing routines. The drawRect method takes a CGRect argument, which indicates the area to draw in. However, the actual drawing commands are performed on a Core Graphics context, which is represented by the CGContext class and can be obtained by a call to UIGraphicsGetCurrentContext.

The Core Graphics context represents a drawable area in iOS, and it is used to print as well as draw graphics. Each view has the responsibility to draw itself; the rectangle will either be the full area (for example, the first time that a view is drawn) or it may be a subset of the area (for example, when a dialog has been displayed and then subsequently removed).

Core Graphics is a C-based interface (rather than Objective-C-based), so the API is exposed as a set of functions beginning with the UIGraphics prefix. As with other drawing APIs, the program can set the...