2012-02-23 71 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. 
相關問題