在UIView子类中调用drawRect [英] calling drawRect in a UIView subclass

查看:121
本文介绍了在UIView子类中调用drawRect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将UIView子类化了,并且我实现了一个drawRect。我基本上希望在设置CGPoint之后调用这个drawRect,这是这​​个UIView的属性。我该怎么做呢?这是我的代码:

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

当我设定这个UIView和我在我的其他UIViewController中调用drawRect,如下所示:

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

为什么会这样?

推荐答案

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

使用 -setNeedsDisplay 代替:

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

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

If you need to customize the drawing area (aka the CGRect that will be passed to -drawRect:), you can use -setNeedsDisplayInRect:

@rob mayoff 所建议,您可以覆盖设置者点_ 属性:

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];
   }
}

这篇关于在UIView子类中调用drawRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆