【问题标题】:NSView won't draw through bezierpathNSView 不会通过 bezierpath 绘制
【发布时间】:2013-08-22 05:10:24
【问题描述】:

亲爱的 Cocoa 程序员们,

我想要完成的事情:

我的画布上有一个复选框、一个 popUpButton(它是隐藏的)和一个 NSView。 如果选中 myCheckbox -> 显示 popUpButton 并通过 NSView 上的 bezierPath 画一条线。 如果 myCheckbox 未选中 -> 再次隐藏 popUpButton 并“取消绘制”路径

代码:

- (IBAction)isChecked:(id)sender {
  //if myChekcbox is checked, show the pop up button
  if ([sender state]==NSOnState) {
    NSLog(@"Checked");
    [myPopUp setHidden:NO];
  }
  else
  {
    //if the checkbox is unchecked, hide the popupbutton
    [myPopUp setHidden:YES];
    NSLog(@"Unchecked");

  }
  //reload my drawrect method (reload the view)
  [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect
{
  //if the checkedbutton is checked, draw the line
  if ([myCheckbox state]==NSOnState)
  {
    NSBezierPath *myPath = [NSBezierPath bezierPath];
    [myPath moveToPoint:NSMakePoint(10, 20)];
    [myPath lineToPoint:NSMakePoint(50, 20)];
    [myPath setLineWidth:2];
    [myPath stroke];
  }

}

问题:

如果检查状态 = NSOnState,popUpButton 是可见的,但线条不会绘制,我想知道为什么......我个人认为这是一个连接问题。

我在这里上传了项目文件(相当小,35kb):Drawing.zip

全球: 我已经阅读了 NSView 文档,它说只有一种方法可以绘制到视图,它是通过 drawRect 方法。这是真的吗?这也是一种绘制视图的下降方式吗? (如果视图中的函数和方法中的 setNeedsDisplay:YES)

提前致谢, 本

【问题讨论】:

  • 发现问题:这是一个连接问题,而自我 setNeedsDisplay: YES 应该是:myView setNeedsDisplay:YES 很傻!感谢您的帮助

标签: cocoa nsview nsbezierpath


【解决方案1】:

您将需要获得一个NSColor 实例,然后在其上调用setStroke 以设置当前笔触颜色。它不知道在drawRect:开头的路径用什么颜色来描边,所以你必须告诉它。

【讨论】:

  • Jesper,代码可以运行,但它不会“解决问题”héhé。我刚刚将 Drawing.zip 更改为正确的链接,请原谅。
  • 这很奇怪,因为画线时没有 IF 语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 2011-04-26
  • 2020-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多