Graphics Reference
In-Depth Information
}
@end
Sometimes it's not viable to replace Core Graphics drawing with a
CAShapeLayer
or one of
the other vector graphics layers. Consider our drawing app: The current implementation
uses hard, straight lines, ideally suited to vector drawing. But suppose we enhance the app
so that it resembles a chalkboard, with a chalk-like texture applied to our lines. A simple
way to simulate chalk is to use a small “brush stroke” image and paste it onto the screen
wherever the user's finger touches, but that approach isn't achievable using a
CAShapeLayer
.
We could create an individual layer for each brush stroke, but this would perform very
badly. The practical upper limit for the number of layers onscreen simultaneously is at most
a few hundred, and we would quickly exceed that. This is the sort of situation where we
have little recourse but to use Core Graphics drawing (unless we want to do something
far
more complex using OpenGL).
Our initial chalkboard implementation is shown in Listing 13.3. We've modified the
DrawingView
from our earlier example to use an array of stroke positions instead of a
UIBezierPath
. Figure 13.2 shows the result.
Listing 13.3
A Simple Chalkboard-Style Drawing App
#import
"DrawingView.h"
#import
<QuartzCore/QuartzCore.h>
#define BRUSH_SIZE
32
@interface
DrawingView
()
@property
(
nonatomic
,
strong
)
NSMutableArray
*strokes;
@end
@implementation
DrawingView
- (
void
)awakeFromNib
{
//create array
self
.
strokes
= [
NSMutableArray
array
];