I'm having problems getting the subject calls to work. The below test should draw two orange and one green rectangle.
Here is my understanding of the below code...
- I draw a orange rectangle at 50,50
- I call the draw greenRect at 200,200, sending the current context
- I push the current context on the stack, change the stroke color and draw a green rect at 100,100
- I pop the current context which should restore the original context (orange stroke color)
- I then draw the last rectangle which should be stroking orange
The last rectangle should stroke orange, but is stroking green, telling me that I modified the original context
Thoughts?
- (void)drawRect:(CGRect)rect{
CGRect aRectangle=CGRectMake(50., 50., 40., 40.);
UIBezierPath *path=[UIBezierPath bezierPathWithRect:aRectangle];
UIColor *strokeColor=[UIColor orangeColor];
[strokeColor setStroke];
[path stroke];
CGContextRef context=UIGraphicsGetCurrentContext();
[self drawGreenRect:context];
CGRect anotherRectangle=CGRectMake(100., 100., 40., 40.);
UIBezierPath *anotherPath=[UIBezierPath bezierPathWithRect:anotherRectangle];
[anotherPath stroke];
}
- (void)drawGreenRect:(CGContextRef)ctxt{
UIGraphicsPushContext(UIGraphicsGetCurrentContext());
CGRect aRectangle=CGRectMake(200., 200., 40., 40.);
UIBezierPath *path=[UIBezierPath bezierPathWithRect:aRectangle];
UIColor *strokeColor=[UIColor greenColor];
[strokeColor setStroke];
[path stroke];
UIGraphicsPopContext();
}