I am making a drawing app for iOS and I have the following classes:
CanvasViewController
holds aCanvasView
and allows you to select aBrush
for use in drawingCanvasView
is aUIView
that contains a background color and an array ofStroke
that are rendered by wiring uptouches
events anddrawRect
Stroke
is anNSObject
that contains aUIBezierPath *path
and aBrush
Brush
contains anint brushType
that is defined by atypedef
and can be things likeBrushTypeSolid, BrushTypeSpray, BrushTypePattern
Initially I thought about how I would handle drawing different brushType
in my drawRect
and it would be something like the following:
in drawrect
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContext(self.frame.size);
if ([squigglesArray count] > 0) {
for (WDSquiggle *squiggle in squigglesArray) {
[self drawSquiggle:squiggle inContext:context];
}
}
[self drawSquiggle:currentSquiggle inContext:context];
UIGraphicsEndImageContext();
in drawSquiggle
switch (squiggle.brushType): {
case BrushTypeSolid:
//solid brush stuff
break;
case BrushTypeX:
//x stuff
break;
}
However, now the drawing logic is all handled in a way that strongly ties the CanvasView
and the BrushType
together.
Is there an elegant way to encapsulate drawing logic in the BrushType
or the Squiggle
so that I could do something like:
[squiggle drawInRect:myRect]
or
[squiggle drawInView:myView]
Or, is this a stupid goal to have / I do not understand encapsulation?