问题描述
我使用下面的代码绘制一条虚线
I used codes below to draw a dashed line
// get the current CGContextRef for the view
CGContextRef currentContext =
(CGContextRef)[[NSGraphicsContext currentContext]
graphicsPort];
// grab some useful view size numbers
NSRect bounds = [self bounds];
float width = NSWidth( bounds );
float height = NSHeight( bounds );
float originX = NSMinX( bounds );
float originY = NSMinY( bounds );
float maxX = NSMaxX( bounds );
float maxY = NSMaxY( bounds );
float middleX = NSMidX( bounds );
float middleY = NSMidY( bounds );
CGContextSetLineWidth( currentContext, 10.0 );
float dashPhase = 0.0;
float dashLengths[] = { 20, 30, 40, 30, 20, 10 };
CGContextSetLineDash( currentContext,
dashPhase, dashLengths,
sizeof( dashLengths ) / sizeof( float ) );
CGContextMoveToPoint( currentContext,
originX + 10, middleY );
CGContextAddLineToPoint( currentContext,
maxX - 10, middleY );
CGContextStrokePath( currentContext );
这是静态的。
和间隙可移动
从右向左移动和圈子
有可能吗?
更多改善的情况:
破折号和间距自动顺时针移动
dashes and gaps move clockwise automatically
欢迎任何评论
推荐答案
最简单的方法是使阶段
变量为ivar, code> keyDown:
The simplest way would be to make the phase
variable an ivar and override keyDown:
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void)keyDown:(NSEvent *)theEvent
{
switch ([theEvent keyCode])
{
case 0x7B: //left cursor key
dashPhase += 10.0;
break;
case 0x7C: //right cursor key
dashPhase -= 10.0;
break;
default:
[super keyDown:theEvent];
break;
}
[self setNeedsDisplay:YES];
}
还要确保将窗口的initialResponder设置为自定义视图假设你在一个NSView子类中绘制)。
Also make sure to set the initialResponder of your window to the custom view (I assume you do your drawing in an NSView subclass).
包围代码不应太难。只需划分你的 dashLengths
数组,并按你想要的方式重新组装。 (您没有指定是否要拆分单个破折号)
The wrapping around code shouldn't be too hard. Just divide your dashLengths
array and reassemble it the way you want. (You didn't specify if you want to split single dashes or not)
更新
OK。我误解了从右到左和圆圈的问题的一部分。我想你想让虚线环绕。如果你想画一个可移动的,虚线的边框,甚至会更容易。把这放在一个NSView子类,它应该绘制一个虚线矩形移动它的破折号,当你按←或→:
OK. I misunderstood the "move from right to left and circle" part of your question. I thought you want the dashed line to wrap around. If you want to draw a rect with a movable, dashed border that would even be easier. Put this in an NSView subclass and it should draw a dashed rectangle that moves it's dashes when you press ← or →:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
patternRectangle = [self bounds];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSetLineWidth( currentContext, 10.0 );
CGFloat dashLengths[] = { 20, 30, 40, 30, 20, 10 };
CGContextSetLineDash( currentContext, dashPhase, dashLengths, sizeof( dashLengths ) / sizeof( float ) );
CGPathCreateWithRect(CGRectMake(2.0, 2.0, 100.0, 100.0), NULL);
CGContextStrokeRect(currentContext, CGRectInset(NSRectToCGRect([self bounds]), 10.0, 10.0));
CGContextStrokePath( currentContext );
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void)keyDown:(NSEvent *)theEvent
{
switch ([theEvent keyCode])
{
case 0x7B:
dashPhase += 10.0;
break;
case 0x7C:
dashPhase -= 10.0;
break;
default:
[super keyDown:theEvent];
break;
}
[self setNeedsDisplay:YES];
}
这篇关于如何使虚线可移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!