Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

John Baughman

macrumors regular
Original poster
Oct 27, 2003
100
0
In assignment 3 of the Stanford course the extra credit portion wants you to give the use the option to display a polygon in a view with either a solid line or a dashed line. It also wants you to fill the polygon with a gradient. I have successfully done both but not at the same time as the gradient appears to be covering the path of the polygon so you cannot see the dashed line when it is selected. If I do not include the gradient the dashed line is visible.

Here is my code for the gradient...

Code:
        ...
        ...
        //code to create the polygon path
        ...
        ...
       CGContextClosePath(context);

        //setup the gradient
	CGGradientRef gradientFill;
	CGColorSpaceRef colorSpace;
	size_t num_locations = 2;
	CGFloat locations[2] = { 0.0, 1.0 };
	CGFloat components[8] = { 0.95, 0.30, 0.30, 1.0,  // Start color
	0.93, 0.94, 0.30, 1.0 }; // End color
	
        //create the gradient
	colorSpace = CGColorSpaceCreateDeviceRGB();
	gradientFill = CGGradientCreateWithColorComponents (colorSpace, components,
													  locations, num_locations);
        int gradientXStart, gradientXEnd;
	
	CGPoint myStartPoint, myEndPoint;
	myStartPoint.x = gradientXStart;   //I set gradientXStart x value of left most point of the polygon while I was creating the path.
	myStartPoint.y = rect.size.height / 2.0;
	myEndPoint.x = gradientXEnd;      //I set gradientXEnd x value of right most point of the polygon while I was creating the path.
	myEndPoint.y = rect.size.height / 2.0;

	CGContextClip(context);
	CGContextDrawLinearGradient (context, gradientFill, myStartPoint, myEndPoint, 0);
	
        //set the line as a dashed line
	[[UIColor blackColor] setStroke];
        CGFloat dash1[] = {10.0, 10.0, 20.0, 30.0, 50.0};
	CGContextSetLineWidth(context, 2.0);
	CGContextSetLineDash(context, 0.0, dash1, 2);
	
	CGContextStrokePath(context);

What can I do to get the gradient fill to not overly the line of the path so that a dashed line will display.

Thanks,

Jonn
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.