2012-02-23 72 views
0

我有一个UIView的子类,我有一个drawRect实现。我基本上想要在设置CGPoint后调用这个drawRect,这是这​​个UIView的一个属性。我该怎么做呢?这里是我的代码:在UIView子类中调用drawRect

-(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); 
    } 
} 

当我设置这个UIView的点和我打电话的drawRect我在其他的UIViewController喜欢如下:

self.page_.point_ = CGPointMake(-100, self.title_.frameHeight + self.title_.frameX + 40); 
    [self.page_ drawRect:self.page_.frame]; 

它给了我所有的这些错误:

<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 

这是为什么?

回答

4

你永远不应该直接打电话给-drawRect:

使用-setNeedsDisplay代替:

self.page_.point_ = CGPointMake(-100, self.title_.frameHeight + self.title_.frameX + 40); 
[self.page_ setNeedsDisplay]; 

如果您需要自定义绘图区域(也就是将传递到-drawRect:CGRect),你可以使用-setNeedsDisplayInRect:

由于@rob mayoff曾建议,您可以覆盖point_属性的设置程序:

- (void)setPoint_:(CGPoint)point { 
    if(!CGPointEqualToPoint(point_, point)) { 
     point_ = point; 
     [self setNeedsDisplay]; 
    } 
} 
+1

如果'point_'的值实际发生了变化,那么'point_'属性setter调用'setNeedsDisplay'会更好。 – 2012-02-23 04:46:42

+0

aha ..这是一个好主意..所以在我的setteer中我会打电话给setNeedsDisplay。介意解释为什么这样更好? – adit 2012-02-23 05:07:04

0

将您的图纸代码包装成 UIGraphicsPushContext(context)UIGraphicsPopContext()以避免此错误消息。

2
- (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. 
相关问题