Assignment 3 – extra credit 4

This content has 13 years. Please, read this page keeping its age in your mind.

For those that trying to implement the Hello Polly (Part II) Assignment  (winter 2010)

here is a hint for:

Fill the background of the custom view or the interior of the polygon with a gradient

- (void)drawRect:(CGRect)rect
{

CGContextRef context = UIGraphicsGetCurrentContext();

[[UIColor lightGrayColor] setFill];
UIRectFill([self bounds]);

NSArray *myPolygonArray = [PolygonView pointsForPolygonInRect:[self bounds]
numberOfSides:[polygon numberOfSides]];

CGMutablePathRef path = CGPathCreateMutable();
NSUInteger index = 0;
for (NSValue *theValue in myPolygonArray) {
if (index == 0) {
CGPathMoveToPoint(path, nil, [theValue CGPointValue].x, [theValue CGPointValue].y);
}
if (index != 0) {
CGPathAddLineToPoint(path, nil, [theValue CGPointValue].x, [theValue CGPointValue].y);
}

if (++index >= [myPolygonArray count]) {
break;
}

}

CGPathCloseSubpath(path);
CGContextAddPath(context, path);

[[UIColor blackColor] setStroke];

CGContextDrawPath(context, kCGPathStroke);
CGContextAddPath(context, path);
CGContextClip(context);

//Prepare the gradient

CGGradientRef gradient;
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = { 204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00,
29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00,
0.0 / 255.0,  50.0 / 255.0, 126.0 / 255.0, 1.00};
gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, nil,
sizeof(components)/(sizeof(components[0])*4));
CGColorSpaceRelease(rgbColorspace);
CGPoint start = CGPointMake(self.bounds.origin.x, self.bounds.origin.y);
CGPoint end = CGPointMake(self.bounds.origin.x, self.bounds.size.height);
CGContextDrawLinearGradient(context, gradient, start, end, 0);

[polygonLabel setText:[polygon name]];
}