我无法在NSView级别的可可中找到任何线条绘制图元。我唯一发现的是NSBezierPath
。这是一个首选的方式?还是有另一种我无法发现的方式?在NSView-drawRect:方法中绘制直线的首选/推荐方法是什么?
7
A
回答
7
Cocoa使用隐式绘图堆栈和无效模型。在你的NSView中,当状态改变会导致视图绘制不同时,你调用 - [self setNeedsDisplay:]告诉绘图系统你需要重画。在不久的将来,实际上是当前事件循环的结尾,您的视图的drawRect:方法将被调用。这是你绘制任何你想要的东西的机会。
有一个隐含的焦点堆栈,这意味着当您调用view的drawRect:时,绘图会聚焦在您的视图的边界上并将其剪裁到其所在的窗口中。然后可以调用函数,如[[NSColor redColor]组];和NSRectFill([自我界限]);
下面是一个例子:
@interface MyView : NSView {
@private
NSColor *lineColor;
NSInteger clickCount;
}
@end
@implementation MyView
- (void)setLineColor:(NSColor *)color {
if (color != lineColor) {
[lineColor release];
lineColor = [color copy];
[self setNeedsDisplay:YES]; /// We changed what we'd draw, invalidate our drawing.
}
}
- (void)mouseDown:(NSEvent *)mouseDown {
clickCount = (clickCount == 6) ? 0 : (clickCount + 1);
CGFloat hue = clickCount/6.0;
[self setLineColor:[NSColor colorWithCalibratedHue:hue saturation:1.0 brightness:1.0 alpha:1.0]];
}
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *line = [NSBezierPath bezierPath];
[line moveToPoint:NSMakePoint(NSMinX([self bounds]), NSMinY([self bounds]))];
[line lineToPoint:NSMakePoint(NSMaxX([self bounds]), NSMaxY([self bounds]))];
[line setLineWidth:5.0]; /// Make it easy to see
[[self lineColor] set]; /// Make future drawing the color of lineColor.
[line stroke];
}
@end
的视图应该绘制一条对角线,它被点击,每次行应该改变颜色。
13
NSBezierPath
正是你应该使用的。如果你只是想从一个点绘制一条直线到另一个,使用类方法:
+strokeLineFromPoint:(NSPoint)point1 toPoint:(NSPoint)point2
2
我试图通过乔恩给出的例子,我发现我需要2条小的修改添加到代码示例以上。
- ,使之成为一个lineToPoint
一旦我固定这一点,我发现代码片段非常有用的插入NSColor的分配器到初始块
@interface PropertyPropagateView : NSView {
@private
NSColor *lineColor;
NSInteger clickCount;
}
@end
@implementation PropertyPropagateView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
lineColor=[NSColor blueColor];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)setLineColor:(NSColor *)color {
if (color != lineColor) {
[lineColor release];
lineColor = [color copy];
[self setNeedsDisplay:YES]; /// We changed what we'd draw, invalidate our drawing.
}
}
- (void)mouseDown:(NSEvent *)mouseDown {
clickCount = (clickCount == 6) ? 0 : (clickCount + 1);
CGFloat hue = clickCount/6.0;
[self setLineColor:[NSColor colorWithCalibratedHue:hue saturation:1.0 brightness:1.0 alpha:1.0]];
}
- (void)drawRect:(NSRect)dirtyRect
{
NSBezierPath *line = [NSBezierPath bezierPath];
[line moveToPoint:NSMakePoint(NSMinX([self bounds]), NSMinY([self bounds]))];
[line lineToPoint:NSMakePoint(NSMaxX([self bounds]), NSMaxY([self bounds]))];
[line setLineWidth:5.0]; /// Make it easy to see
[lineColor set]; /// Make future drawing the color of lineColor.
[line stroke];
}
@end
1
只是为了添加一些信息,我习惯于确保图形状态在绘制之前和之后保存和恢复,以保持活动的活力。
- (void)drawRect:(NSRect)dirtyRect {
[[NSGraphicsContext currentContext] saveGraphicsState]
NSBezierPath *line = [NSBezierPath bezierPath];
[line moveToPoint:NSMakePoint(NSMinX([self bounds]), NSMinY([self bounds]))];
[line lineToPoint:NSMakePoint(NSMaxX([self bounds]), NSMaxY([self bounds]))];
[line setLineWidth:5.0]; /// Make it easy to see
[[self lineColor] set]; /// Make future drawing the color of lineColor.
[line stroke];
[[NSGraphicsContext currentContext] restoreGraphicsState]
}
+1
为什么这会让事情变得活泼? – 2015-07-24 05:26:45
相关问题
- 1. 使用raphael.js绘制直线的正确方法是什么?
- 2. 在C++中处理Winsock错误的推荐方法是什么?
- 3. 什么是在VS 2010中构建MSI包的推荐方法?
- 4. 在ItemizedOverlay的绘制方法中获取变量值的首选方法是什么?
- 5. 返回类型推理:什么方法是首选?
- 6. 推荐的方法在PHP
- 7. 在Intershop上解析JSON的推荐方法是什么?
- 8. RoR中应用程序设置的推荐方法是什么?
- 9. 跨线程编组COM接口的首选方法是什么?
- 10. 注释中注释方法的首选方式是什么?
- 11. 什么是建立在线3D游戏引擎的推荐方法?
- 12. 在C++中实现工厂方法的首选方法是什么?
- 13. 现代D的首选控制台输出方法是什么?
- 14. 什么是绘制线阵列的最快方法?
- 15. 在SSRS中生成数据集的首选方法是什么?
- 16. 在.Net中构建Web服务的首选方法是什么?
- 17. 在表格中更新字段的首选方法是什么?
- 18. 在猫鼬中定义数组的首选方法是什么?
- 19. 不推荐使用Springfox ClassOrApiAnnotationResourceGrouping类的替代方法是什么?
- 20. 推荐部署Yesod的方法是什么?
- 21. 什么是推荐使用Subsonic的方法
- 22. 与片段活动交流的推荐方法是什么?
- 23. 什么是写这个SQL语句的推荐方法?
- 24. 什么是存储SharePoint Web部件图像的推荐方法?
- 25. 调用Oracle MCS API的推荐方法是什么?
- 26. REST PUT方法的推荐/有效请求负载是什么?
- 27. 运输机器学习模型的推荐方法是什么?
- 28. 一个NServiceBus.Host为一个EndPoint,什么是推荐的方法
- 29. 在Python中创建和管理线程的首选方法是什么?
- 30. 什么是当前和推荐的方法来消除异步线程?
darRect方法中的第二个moveToPoint调用应该是lineToPoint调用。 – 2012-10-18 05:24:41
完成! - 谢谢。 – 2012-10-18 05:33:45