我有一個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
這是爲什麼?
如果'point_'的值實際發生了變化,那麼'point_'屬性setter調用'setNeedsDisplay'會更好。 – 2012-02-23 04:46:42
aha ..這是一個好主意..所以在我的setteer中我會打電話給setNeedsDisplay。介意解釋爲什麼這樣更好? – adit 2012-02-23 05:07:04