gpt4 book ai didi

macos - 如何使虚线可移动

转载 作者:行者123 更新时间:2023-12-03 16:20:37 24 4
gpt4 key购买 nike

我使用下面的代码来绘制虚线

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

它是静态的。

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

从右向左移动并画圈

可能吗?

更多改进案例:

enter image description here

虚线和间隙自动顺时针移动

欢迎大家留言

最佳答案

最简单的方法是使 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 设置为自定义 View (我假设您在 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];
}

关于macos - 如何使虚线可移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7911612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com