如何使虚线可移动

发布于 2024-12-12 01:37:18 字数 1251 浏览 0 评论 0原文

我使用下面的代码绘制了一条虚线

// 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 );

enter image description here

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:

enter image description here

dashes and gaps move clockwise automatically

Welcome any comment

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-12-19 01:37:18

最简单的方法是使 phase 变量成为 ivar 并覆盖 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 子类中绘制)。

环绕代码应该不会太难。只需划分您的 dashLengths 数组并按照您想要的方式重新组合即可。 (您没有指定是否要分割单个破折号)

更新

确定。我误解了你的问题的“从右向左移动并圈出”部分。我以为你想让虚线环绕起来。如果你想绘制一个带有可移动虚线边框的矩形,那就更容易了。将其放入 NSView 子类中,它应该绘制一个虚线矩形,当您按 ← 或 → 时,该矩形会移动其破折号:

- (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];
}

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];
}

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 →:

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文