如何用 NSBezierPath 绘制平滑路径
我目前正在使用 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];
}
但对我来说,注意到变化,我仍然得到了丑陋的路径。
有什么建议吗?
感谢您的回答。
详细信息如下:
- 创建一个可可应用程序(不是基于文档的)
- 创建一个新类“StretchView”
StretchView.h
#import <Cocoa/Cocoa.h>
@interface StretchView : NSView {
NSBezierPath *path;
}
-(void)myTimerAction:(NSTimer *) timer;
@end
StretchView.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.选择“Scroll view”并设置类“StretchView”(在类标识窗口中)
I am draing path with NSBezierPath currently:
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor blueColor] set];
[path stroke];
}
But the line seems pretty ugly(not smooth)
Then I googled for some solutions and I got this:
- (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];
}
But for me, noting changed, I still got the ugly path.
Any suggestion?
Thanks for answer.
Here's the details:
- Create a cocoa application (not document-based)
- Create a new class "StretchView"
StretchView.h
#import <Cocoa/Cocoa.h>
@interface StretchView : NSView {
NSBezierPath *path;
}
-(void)myTimerAction:(NSTimer *) timer;
@end
StretchView.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.Open the Interface builder and drag a "Scroll view" to the main window
4.Choose the "Scroll view" and set the class "StretchView" (in class identity window)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 lineToPoint 只会创建锯齿形直线,即使作为贝塞尔曲线路径的一部分也是如此。如果“平滑”指的是“曲线”,那么您可能应该查看
curveToPoint
。另请注意,这些“toPoint”方法已经移动了该点,无需moveToPoint
除非您打算移动到与线结束位置不同的点。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 outcurveToPoint
instead. Also, note that these "toPoint" methods already move the point, no need tomoveToPoint
unless you intend to move to a different point than where your line ended.