Graphics Reference
In-Depth Information
Figure 13.1 A simple sketch produced using Core Graphics
The problem with this implementation is that the more we draw, the slower it gets. Because
we are redrawing the entire UIBezierPath every time we move our finger, the amount of
drawing work that needs to be done increases every frame as the path becomes more
complex, causing the frame rate to plummet. We need a better approach.
Core Animation provides specialist classes for drawing these types of shape with hardware
assistance (as covered in detail in Chapter 6, “Specialized Layers”). Polygons, lines, and
curves can be drawn using CAShapeLayer . Text can be drawn using CATextLayer .
Gradients can be drawn using CAGradientLayer . These will all be substantially faster than
using Core Graphics, and they avoid the overhead of creating a backing image.
If we modify our drawing app to use CAShapeLayer instead of Core Graphics, the
performance is substantially improved (see Listing 13.2). Performance will inevitably still
degrade as the complexity of the path increases, but it would now take a very complex
drawing to make an appreciable difference to the frame rate.
Listing 13.2 Reimplementing the Drawing App Using CAShapeLayer
#import "DrawingView.h"
#import <QuartzCore/QuartzCore.h>
@interface DrawingView ()
@property ( nonatomic , strong ) UIBezierPath *path;
@end
Search WWH ::




Custom Search