如何使虚线可移动
我使用下面的代码绘制了一条虚线
// 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 );
它是静态的。
但我更喜欢使破折号和间隙可
从右向左移动并圈
出可能吗?
更多改进案例:
破折号和间隙自动顺时针移动
欢迎任何评论
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 );
it is static.
But I prefer to make the dashes and gaps moveable
move from right to left and circle
Is it possible?
more improved case:
dashes and gaps move clockwise automatically
Welcome any comment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是使
phase
变量成为 ivar 并覆盖keyDown:
另外,请确保将窗口的initialResponder 设置为自定义视图(我假设您执行了您的操作)在 NSView 子类中绘制)。
环绕代码应该不会太难。只需划分您的
dashLengths
数组并按照您想要的方式重新组合即可。 (您没有指定是否要分割单个破折号)更新
确定。我误解了你的问题的“从右向左移动并圈出”部分。我以为你想让虚线环绕起来。如果你想绘制一个带有可移动虚线边框的矩形,那就更容易了。将其放入 NSView 子类中,它应该绘制一个虚线矩形,当您按 ← 或 → 时,该矩形会移动其破折号:
The simplest way would be to make the
phase
variable an ivar and overridekeyDown:
Also make sure to set the initialResponder of your window to the custom view (I assume you do your drawing in an NSView subclass).
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)Update
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 →: