Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 使用NSBezierPath时切换线路属性_Cocoa_Graphics - Fatal编程技术网

Cocoa 使用NSBezierPath时切换线路属性

Cocoa 使用NSBezierPath时切换线路属性,cocoa,graphics,Cocoa,Graphics,在使用NSBezierPath绘制图表的不同线条时,我在更改线条颜色/宽度时遇到了一些非常基本的问题。下面的代码应该清楚地说明我正在尝试做什么: #import "DrawTest.h" @implementation DrawTest - (id)initWithFrame:(NSRect)frameRect { NSLog(@"in 'initWithFrame'..."); if ((self = [super initWithFrame:frameRect]) != nil)

在使用NSBezierPath绘制图表的不同线条时,我在更改线条颜色/宽度时遇到了一些非常基本的问题。下面的代码应该清楚地说明我正在尝试做什么:

#import "DrawTest.h"

@implementation DrawTest

- (id)initWithFrame:(NSRect)frameRect
{
NSLog(@"in 'initWithFrame'...");
    if ((self = [super initWithFrame:frameRect]) != nil)
        {
    [self drawBaseline];
    [self display]; // ...NO!
    [self drawPlotline];
    }
return self;
}

- (void)drawBaseline
{
    NSRect windowRect;
    int width, height;

    windowRect = [self bounds];
    width = round(windowRect.size.width);
    height = round(windowRect.size.height);

    theLineWidth=1.0;
    [[NSColor grayColor] set];
    // allocate an instance of 'NSBezierPath'...
    path = [[NSBezierPath alloc]init];
    // draw a HORIZONTAL line...
    [path moveToPoint:NSMakePoint(0,height/2)];
    [path lineToPoint:NSMakePoint(width,height/2)];
}

- (void)drawPlotline
{
    theLineWidth=10.0;
    [[NSColor redColor] set];
    // draw a VERTICAL line...
    [path moveToPoint:NSMakePoint(100,125)]; // x,y
    [path lineToPoint:NSMakePoint(100,500)];
}

- (void)drawRect:(NSRect)rect
{
NSLog(@"in 'drawRect'...");
    // draw the path line(s)
    //  [[NSColor redColor] set];
    [path setLineWidth:theLineWidth];
    [path stroke];
}

- (void)dealloc
{
    [path release];
    [super dealloc];
}

@end
问题显然在于,两个方法运行后,“drawRect”只会被调用一次,结果是所有线条都出现在最后一个颜色和线宽集中。我尝试调用[self display]等,希望强制'drawRect'在两个方法调用之间重新绘制NSView内容,但没有效果

有谁能提出一个基本的策略来实现我在这里要做的事情吗?任何帮助都将不胜感激。

快速回答是:将[self-DrawSeline]和[self-drawPlotline]移动到drawRect内。 另外,在更改颜色之前,您需要为每种颜色调用一次[path stroke]。因此,伪代码类似于

-(void)drawRect:(NSRect)rect
{
      NSBezierPath*path1=...
      construct the path for color red...
      [[NSColor redColor] set];
      [path1 stroke]; 

      NSBezierPath*path2=...
      construct the path for color blue...
      [[NSColor blueColor] set];
      [path2 stroke]; 

}
记住:

[path moveToPoint:]等不绘制路径。它只是在对象内部构造路径。构建路径后,使用[路径笔划]笔划路径

此外,颜色不是NSBezierPath实例内部构造的路径的属性。因此,[颜色集]不会记录在NSBezierPath实例中!这是图形上下文的属性。从概念上讲,1。您可以构建路径。2.将颜色设置为上下文。3.你划过这条路

在Cocoa中,不是告诉系统何时绘制、何时刷新等。Cocoa通过调用drawRect告诉您何时绘制:。您绘制的图形上下文在drawRect:之外不可用,除非您自己创建屏幕外上下文。所以,不要麻烦事先画。只需在drawRect中绘制所有内容

快速回答是:在drawRect内移动[self-DrawSeline]和[self-drawPlotline]。 另外,在更改颜色之前,您需要为每种颜色调用一次[path stroke]。因此,伪代码类似于

-(void)drawRect:(NSRect)rect
{
      NSBezierPath*path1=...
      construct the path for color red...
      [[NSColor redColor] set];
      [path1 stroke]; 

      NSBezierPath*path2=...
      construct the path for color blue...
      [[NSColor blueColor] set];
      [path2 stroke]; 

}
记住:

[path moveToPoint:]等不绘制路径。它只是在对象内部构造路径。构建路径后,使用[路径笔划]笔划路径

此外,颜色不是NSBezierPath实例内部构造的路径的属性。因此,[颜色集]不会记录在NSBezierPath实例中!这是图形上下文的属性。从概念上讲,1。您可以构建路径。2.将颜色设置为上下文。3.你划过这条路

在Cocoa中,不是告诉系统何时绘制、何时刷新等。Cocoa通过调用drawRect告诉您何时绘制:。您绘制的图形上下文在drawRect:之外不可用,除非您自己创建屏幕外上下文。所以,不要麻烦事先画。只需在drawRect中绘制所有内容


你肯定需要反复阅读Cocoa绘图指南,因为你从根本上误解了Cocoa绘图的工作原理。正如Yuji所指出的,只有当Cocoa告诉你的时候,你的观点才会吸引人。initWithFrame:中的绘图代码没有执行任何操作,因为您的视图还没有在窗口中,因为它甚至还没有完成初始化。除了Rob Keniger已经提到的Cocoa绘图指南外,您还需要阅读Cocoa的视图编程指南。谢谢罗布和彼得。是的,我会看文档的。不幸的是,我的许多问题都源于这样一个事实:1我仍在努力摆脱旧的“proc-C”习惯;2在65岁时,我的大脑不再像过去那样与我合作:-感谢你的两个有用的输入。你是我年龄的两倍!好好干!高在木下阿里加托-你肯定需要反复阅读Cocoa绘图指南,因为你从根本上误解了Cocoa绘图的工作原理。正如Yuji所指出的,只有当Cocoa告诉你的时候,你的观点才会吸引人。initWithFrame:中的绘图代码没有执行任何操作,因为您的视图还没有在窗口中,因为它甚至还没有完成初始化。除了Rob Keniger已经提到的Cocoa绘图指南外,您还需要阅读Cocoa的视图编程指南。谢谢罗布和彼得。是的,我会看文档的。不幸的是,我的许多问题都源于这样一个事实:1我仍在努力摆脱旧的“proc-C”习惯;2在65岁时,我的大脑不再像过去那样与我合作:-感谢你的两个有用的输入。你是我年龄的两倍!好好干!高在木下阿里加托-谢谢Yuji,我感谢你的帮助。它现在工作得很好:-谢谢Yuji,我感谢你的帮助。现在效果很好:-