0

我目前正在使用 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];
}

但对我来说,注意到改变了,我仍然走上了丑陋的道路。

有什么建议吗?

感谢您的回答。

这是详细信息:

  1. 创建一个可可应用程序(不是基于文档的)
  2. 创建一个新类“StretchView”

拉伸视图.h

#import <Cocoa/Cocoa.h>

@interface StretchView : NSView {

    NSBezierPath *path;
}

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

@end

拉伸视图.m

#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

3.打开界面生成器并将“滚动视图”拖到主窗口
4.选择“滚动视图”并设置类“StretchView”(在类标识窗口中)

4

1 回答 1

0

Using lineToPoint will just create straight zigzaggy lines, even as part of a bezier path. If by "smooth" you mean "curvy", then you should probably check out curveToPoint instead. Also, note that these "toPoint" methods already move the point, no need to moveToPoint unless you intend to move to a different point than where your line ended.

于 2011-06-30T06:03:34.770 回答