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_Nsbezierpath - Fatal编程技术网

Cocoa 如何使用NSBezierPath绘制平滑路径

Cocoa 如何使用NSBezierPath绘制平滑路径,cocoa,nsbezierpath,Cocoa,Nsbezierpath,我正在使用NSBezierPath拖动当前路径: - (void)drawRect:(NSRect)dirtyRect { [[NSColor blueColor] set]; [path stroke]; } 但这条线看起来很糟糕,并不平坦 然后我在谷歌上搜索了一些解决方案,我得到了: - (void)drawRect:(NSRect)dirtyRect { NSGraphicsContext *graphicsContext; BOOL oldShoul

我正在使用NSBezierPath拖动当前路径:

- (void)drawRect:(NSRect)dirtyRect 
{
    [[NSColor blueColor] set];
    [path stroke];
}
但这条线看起来很糟糕,并不平坦

然后我在谷歌上搜索了一些解决方案,我得到了:

- (void)drawRect:(NSRect)dirtyRect 
{
    NSGraphicsContext *graphicsContext;
    BOOL oldShouldAntialias;

    graphicsContext = [NSGraphicsContext currentContext];
    oldShouldAntialias = [graphicsContext shouldAntialias];
    [graphicsContext setShouldAntialias:NO];

    [[NSColor blueColor] set];
    [path stroke];

    [graphicsContext setShouldAntialias:oldShouldAntialias];
}
但对我来说,没有任何改变,我仍然走上了丑陋的道路

有什么建议吗

谢谢你的回答

详情如下:

创建非基于文档的cocoa应用程序 创建一个新的类StretchView StretchView.h 3.打开Interface builder并将滚动视图拖动到主窗口
4.选择滚动视图并在“类别标识”窗口中设置类别StretchView,使用lineToPoint只会创建直的锯齿形线条,即使是作为bezier路径的一部分。如果“平滑”指的是曲线,那么您可能应该检查curveToPoint。另外,请注意,这些toPoint方法已经移动了点,除非您打算移动到与线结束位置不同的点,否则无需移动toPoint。

请附上一个说明问题的屏幕截图。另外,请让我们知道此drawRect方法的位置。它只是一个简单视图,还是类似NSTableView的东西?以及设置路径的代码。您可能希望打开而不是关闭抗锯齿。。。
#import <Cocoa/Cocoa.h>

@interface StretchView : NSView {

    NSBezierPath *path;
}

-(void)myTimerAction:(NSTimer *) timer;

@end
#import "StretchView.h"

@implementation StretchView

- (id)initWithFrame:(NSRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) {

        path = [[NSBezierPath alloc] init]; 
        NSPoint p = CGPointMake(0, 0); 
        [path moveToPoint:p]; 
        myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                   target:self
                                                 selector:@selector(myTimerAction:)
                                                 userInfo:nil
                                                  repeats:YES];
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect 
{
    NSLog(@"drawRect");

    [[NSColor blueColor] set];
    [path stroke];
}

-(void)myTimerAction:(NSTimer *) timer
{  
    NSPoint p = CGPointMake(a, b); //a, b is random int val
    [path lineToPoint:p]; 
    [path moveToPoint:p]; 
    [path closePath];

    [self setNeedsDisplay:YES];
} 

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

@end