绘制矩形上下文错误

发布于 2024-09-04 08:26:16 字数 967 浏览 3 评论 0原文

我听说很多人不使用drawRect 都会遇到上下文错误

现在我有这个:

 - (void)drawRect:(CGRect)rect {  
  NSLog(@"drawRect: Starts");
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
  CGContextSetLineWidth(context, 3.0);
  CGContextMoveToPoint(context, lineStart.x, lineStart.y);
  CGContextAddLineToPoint(context, lineEnd.x, lineEnd.y);
  CGContextStrokePath(context); 
}

错误:

<Error>: CGContextSetRGBStrokeColor: invalid context

它在以前的程序上有效,但在这个程序上无效。有什么不同:我有一个视图控制器,它调用这个 UIView:

-(void)createLine:(CGPoint)start:(CGPoint)end {
 NSLog(@"createLine: Starts");
 lineEnd = start;
 lineStart = end;
 self = [super initWithFrame:drawRect:CGRectMake(fmin(lineStart.x, lineEnd.x), fmin(lineStart.y, lineEnd.y), fabs(lineStart.x - lineEnd.x), fabs(lineStart.y - lineEnd.y))];
}

这是我的第一个问题,我不确定我应该在这里放多少信息代码,所以对我来说容易一些。

I heard that a lot of people get a context error by not using drawRect

Now I have this:

 - (void)drawRect:(CGRect)rect {  
  NSLog(@"drawRect: Starts");
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
  CGContextSetLineWidth(context, 3.0);
  CGContextMoveToPoint(context, lineStart.x, lineStart.y);
  CGContextAddLineToPoint(context, lineEnd.x, lineEnd.y);
  CGContextStrokePath(context); 
}

Error:

<Error>: CGContextSetRGBStrokeColor: invalid context

Which had work on previous programs, but not on this one. Whats different: I have a view controller, which calls this UIView:

-(void)createLine:(CGPoint)start:(CGPoint)end {
 NSLog(@"createLine: Starts");
 lineEnd = start;
 lineStart = end;
 self = [super initWithFrame:drawRect:CGRectMake(fmin(lineStart.x, lineEnd.x), fmin(lineStart.y, lineEnd.y), fabs(lineStart.x - lineEnd.x), fabs(lineStart.y - lineEnd.y))];
}

This is my first question, and I am not sure how much info code I should be putting here so be easy on me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-09-11 08:26:16

冒着陈述显而易见的风险,看起来您在没有有效上下文的情况下有时会调用 createLine 。这就是为什么直接调用drawRect不是一个好主意。那么,调用setNeedsDisplay并让系统在合适的时间调用drawRect

At the risk of stating the obvious, it looks like you're calling createLine sometime when you don't have a valid context. This is why it's not a good idea to call drawRect directly. Intsead, call setNeedsDisplay and let the system call drawRect at a suitable time.

病毒体 2024-09-11 08:26:16

正如 walkytalky 所写,您不应该直接调用 drawRect 。如果您需要确保触发对 drawRect 的调用,请调用 setNeedsDisplay,这将确保在适当的时候调用 drawRect时间。

createLine:: 方法的最后一行有点令人困惑且不正统。我认为出于您的目的,您需要删除该行并将其替换为 all to setNeedsDisplay。这应该会使用新的 lineStart 和 lineEnd 值触发对 drawRect 的调用。

As walkytalky wrote, you should not call drawRect directly. If you need to make sure that a call to drawRect is triggered, then call setNeedsDisplay, which will make sure that drawRect is called at the appropriate time.

The last line of your createLine:: method is a bit confusing and unorthodox. I think that for your purposes that you want to delete that line and simply replace it with a all to setNeedsDisplay. That should trigger a call to drawRect with the new lineStart and lineEnd values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文