Graphics Programs Reference
In-Depth Information
If one view redraws its image, the other views on the screen are not required to redraw
theirs. Instead, their existing images will just be composited on the screen again. This op-
timization is why drawing and animation on iOS feels so responsive.
To see the effect of redrawing a view's image, let's give HypnosisView a circleCo-
lor property. Each circle that the HypnosisView draws will be the color pointed to by
this property. In HypnosisView.h , declare this property.
@interface HypnosisView : UIView
{
}
@property (nonatomic, strong) UIColor *circleColor;
@end
In HypnosisView.m , synthesize this property to automatically create the instance vari-
able, setter, and getter.
@implementation HypnosisView
@synthesize circleColor;
Now, in HypnosisView.m , update the initWithFrame: method to create a default
circleColor .
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
[self setCircleColor:[UIColor lightGrayColor]];
}
return self;
}
Then, in drawRect: , modify the message that sets the context's stroke color to use the
circleColor instead of light gray.
CGContextSetLineWidth(ctx, 10);
[[self circleColor] setStroke];
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
CGContextStrokePath(ctx);
}
Build and run the application. The circles are the same color because we don't have a way
to change circleColor from the default once the application is running. Let's make
Search WWH ::




Custom Search