Reputation: 33644
I have subclassed a UIView and I have a drawRect implemented. I basically want this drawRect to be called after I've set the CGPoint, which is a property of this UIView. How do I do this? Here's my code:
-(id) initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder]) {
point_.x = 0;
self.page_ = [UIImage imageNamed:@"pages"];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
if (point_.x != 0){
[self.page_ drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 1.0f);
CGContextMoveToPoint(context, 40, point_.y);
CGContextAddLineToPoint(context, 420, point_.y);
CGContextStrokePath(context);
}
}
When I've set the point of this UIView and I call drawRect in my other UIViewController like follows:
self.page_.point_ = CGPointMake(-100, self.title_.frameHeight + self.title_.frameX + 40);
[self.page_ drawRect:self.page_.frame];
It gives me all of these errors:
<Error>: CGContextSaveGState: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextSetBlendMode: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextSetAlpha: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextTranslateCTM: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextScaleCTM: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextDrawImage: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextRestoreGState: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextSetLineWidth: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextMoveToPoint: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextAddLineToPoint: invalid context 0x0
Feb 22 20:24:38 <Error>: CGContextDrawPath: invalid context 0x0
Why is this?
Upvotes: 0
Views: 3276
Reputation: 1596
Wrap your drawing code into
UIGraphicsPushContext( context )
and UIGraphicsPopContext()
to avoid this error messages.
Upvotes: 0
Reputation: 3329
- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
if (self = [super initWithCoder:aDecoder])
{
[self setMultipleTouchEnabled:NO]; // (2)
[self setBackgroundColor:[UIColor whiteColor]];
path = [UIBezierPath bezierPath];
[path setLineWidth:2.0];
}
return self;
}
- (void) drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextDrawPath(context, kCGPathFillStroke); //CGContextSetRGBFillColor doesn't work either
CGContextBeginPath(context);
CGContextMoveToPoint(context, 100.0, 60.0);
CGRect rectangle = {100.0, 60.0, 120.0, 120.0};
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
CGContextFillPath(context);
}
this code implement in create new uiview and add this code i hope that code you usefull.
Upvotes: 2
Reputation: 163228
You should never, ever call -drawRect:
directly.
Use -setNeedsDisplay
instead:
self.page_.point_ = CGPointMake(-100, self.title_.frameHeight + self.title_.frameX + 40);
[self.page_ setNeedsDisplay];
If you need to customize the drawing area (aka the CGRect
that will be passed to -drawRect:
), you can use -setNeedsDisplayInRect:
As @rob mayoff has suggested, you can override the setter for the point_
property:
- (void)setPoint_:(CGPoint)point {
if(!CGPointEqualToPoint(point_, point)) {
point_ = point;
[self setNeedsDisplay];
}
}
Upvotes: 4