【问题标题】:how to make dashed line moveable如何使虚线可移动
【发布时间】:2011-10-27 04:07:17
【问题描述】:

我用下面的代码画了一条虚线

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

它是静态的。

但我更喜欢让破折号和间隙可移动

从右向左移动并转圈

有可能吗?

更多改进案例:

破折号和间隙自动顺时针移动

欢迎评论

【问题讨论】:

  • 很棒的问题。我希望迟早要在我的代码中实现的东西只是不知道如何。 +1

标签: macos cocoa core-graphics cgcontext


【解决方案1】:

最简单的方法是将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];
}

【讨论】:

  • 我希望破折号和空白自动移动,请参考修改问题
猜你喜欢
  • 1970-01-01
  • 2021-06-08
  • 2015-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 2019-11-04
相关资源
最近更新 更多