Rob*_*ger 14
NSBezierPath正是你应该使用的.如果您只想从一个点到另一个点绘制直线,请使用类方法:
+strokeLineFromPoint:(NSPoint)point1 toPoint:(NSPoint)point2
Cocoa使用隐式绘图堆栈和失效模型.在NSView中,当状态更改会导致视图绘制方式不同时,您调用 - [self setNeedsDisplay:]告诉绘图系统您需要重绘.在不久的将来的某个时刻,实际上是当前事件循环的结束,将调用视图的drawRect:方法.这是你画出任何你喜欢的东西的机会.
有一个隐含的焦点堆栈,这意味着当你的视图的drawRect:被调用时,绘图会聚焦并剪切到它所在窗口中视图的边界.然后你可以调用像[[NSColor redColor] set]这样的函数; 和NSRectFill([self bounds]);
这是一个例子:
@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
Run Code Online (Sandbox Code Playgroud)
视图应绘制一条对角线,每次单击该线时,该线应改变颜色.
| 归档时间: |
|
| 查看次数: |
11641 次 |
| 最近记录: |