iOS Core Animation: Advanced Techniques (2014)
When Apple engineers created the iPhone, they faced a challenge: they needed to create a modern, fast, and fluid user interface, the likes of which had never been seen outside of a video game, and they needed to do it on mobile hardware that was a decade behind current desktops and laptops in terms of graphics performance.
They also had an opportunity: to rebuild AppKit (the Mac OS user interface framework) from the ground up using modern graphics technology without needing to maintain support for the legacy applications inherited from nearly 25 years of Macintosh and NeXTSTEP history.
Their solution was a private framework called Layer Kit, developed by the iPhone software team to provide a high-performance, hardware-accelerated animation and compositing library to replace the slower, Quartz-based software drawing used by AppKit. This framework actually debuted first on Mac OS 10.5 under the name Core Animation, shortly before the iPhone was announced.
Core Animation is not just a set of functions for performing animations; it lies at the very heart of iOS, powering everything you see on screen. Even if you never invoke it explicitly, you are using it implicitly every time you display a view or transition from one screen to the next. As an iOS developer, you can build great applications without ever consciously touching Core Animation, but if you truly embrace its features, you can achieve much richer user experiences.
The purpose of this book is to demystify Core Animation, to bring it out from behind the curtains and help you to harness its full power to make spectacular applications. By the end, you will have learned exactly where and when to use Core Animation, how to work with and around its limitations, and what to do to avoid performance pitfalls so that your apps can be as responsive as Apple’s own.
Audience and Material
This is neither a beginner’s guide to Cocoa nor an introduction to the iOS platform for Mac developers. It is most definitely not an introduction to programming in general.
This book is written for an audience that is reasonably familiar with Xcode, Cocoa Touch, and UIKit but has no prior knowledge of the Core Animation framework. You need not be a very seasoned developer (perhaps you have just finished your “Hello World” project and are looking for something a bit more advanced), but you will need to be fluent in Objective-C to be able to follow the code examples.
Although no prior knowledge of Core Animation is assumed, this is not merely a cursory introduction to the framework. The aim of this book is to leave no stone unturned when it comes to the Core Animation APIs. Even if you have already used Core Animation for many years in an iOS-specific context, it would be surprising if you did not find things in here that you don’t already know about or fully understand.
This book is specifically geared toward the iOS platform. Where appropriate, differences between Core Animation on iOS and Mac OS are mentioned, but Mac-specific Core Animation features such as CALayoutManager or Core Image integration or are not discussed in detail. If you are already well versed in Core Animation on Mac OS, much of the material will already be familiar to you, but this should serve as a useful conversion guide.
Core Animation has been a key part of iOS since the beginning, and the majority of its features are available on older iOS versions. Any methods or classes that are new to iOS 6 are highlighted as such, but the purpose of this book is to document the current feature set of Core Animation; so with very few exceptions, the task of providing backward compatibility for earlier iOS versions is left as an exercise for the reader.
This book is structured in a linear fashion, with each chapter building upon concepts introduced in the previous ones. That said, wherever we refer back to a concept covered in an earlier chapter, the chapter is referenced explicitly so that you can read the book in a nonlinear fashion if you prefer.
The subject matter is split into three parts, dealing with static content, animation, and performance optimization, respectively. These parts are self-contained, so (for example) if you already know about animation and layout, you can dive straight into the part on performance.
Each chapter contains figures and example code to illustrate the topics discussed. The sample code projects are available for download from www.informit.com/title/9780133440751 if you prefer not to retype them by hand.
Before We Begin
The examples in this book were written and tested using Xcode 4.6 on Mac OS 10.8 (Mountain Lion). The latest version of Xcode can be downloaded free of charge from the Mac App Store, and most of the examples can be run in the iOS simulator. In addition, you need to sign up for a free Apple developer account to access most of the tools and documentation for the classes referenced in the book.
The code examples have all been tested with iOS 6.1, but most will either run unmodified on iOS 5+, or can be trivially modified to do so by removing noncritical iOS 6 features such as Autolayout. All examples make use of modern Objective-C practices such as ARC (Automatic Reference Counting), automatic property synthesis, and object literals and so require Xcode 4.5 and iOS 4 as a minimum.
The examples in the final, performance-focused section of the book must be installed on a physical iPhone 5 running iOS 6.1 to demonstrate the exact behavior described in the text. To run the examples on an iPhone, you need to pay for an iOS developer license, which you can purchase directly from Apple. These examples will still work on the simulator, or a different device or iOS version, but will likely exhibit different performance characteristics.