我正在尝试为this question构建一个测试用例。为此,我需要绘制一个虚线,其中的大写和联接与默认值不同;例如,圆形线帽。 Gelphman和Laden的 Programming with Quartz 一书说我应该能够;第125页讨论了很多关于使用不同的大写和连接绘制虚线的信息,甚至还有一个数字显示了虚线与不同大写字母的外观。
然而,当我跑
时CGFloat lengths[2] = { 5, 3 };
CGContextMoveToPoint(c, 50, 50);
CGContextAddLineToPoint(c, 100, 30);
CGContextAddLineToPoint(c, 150, 70);
CGContextAddLineToPoint(c, 200, 50);
CGContextSetLineWidth(c, 10);
CGContextSetLineJoin(c, kCGLineJoinBevel);
CGContextSetLineCap(c, kCGLineCapRound);
CGContextSetLineDash(c, 0, lengths, 2);
CGContextSetRGBStrokeColor(c, 0, 0, 0, 1);
CGContextStrokePath(c);
我看到一条实线,而不是虚线。如果我注释掉以下两行:
CGContextSetLineJoin(c, kCGLineJoinBevel);
CGContextSetLineCap(c, kCGLineCapRound);
然后我看到了我的潇洒。其中一条线本身会使图形恢复为实线。
我不知道该怎么回事。提供了一个示例程序。这是在OS X 10.10上;我需要追溯到10.7。
这适用于OS X,而非iOS。
感谢。
// 15 october 2015
#import <Cocoa/Cocoa.h>
@interface dashStrokeView : NSView
@end
void putstr(CGContextRef c, const char *str, double x, double y)
{
NSFont *sysfont;
CFStringRef string;
CTFontRef font;
CFStringRef keys[1];
CFTypeRef values[1];
CFDictionaryRef attrs;
CFAttributedStringRef attrstr;
CTLineRef line;
sysfont = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
font = (CTFontRef) sysfont; // toll-free bridge
string = CFStringCreateWithCString(kCFAllocatorDefault,
str, kCFStringEncodingUTF8);
keys[0] = kCTFontAttributeName;
values[0] = font;
attrs = CFDictionaryCreate(kCFAllocatorDefault,
keys, values,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
attrstr = CFAttributedStringCreate(kCFAllocatorDefault, string, attrs);
line = CTLineCreateWithAttributedString(attrstr);
CGContextSetTextPosition(c, x, y);
CTLineDraw(line, c);
CFRelease(line);
CFRelease(attrstr);
CFRelease(attrs);
CFRelease(string);
}
@implementation dashStrokeView
- (void)drawRect:(NSRect)r
{
CGContextRef c;
CGFloat lengths[2] = { 5, 3 };
CGMutablePathRef buildpath;
CGPathRef copy, copy2;
c = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(c);
putstr(c, "Dash + Stroke With CGContext Functions", 10, 10);
CGContextMoveToPoint(c, 50, 50);
CGContextAddLineToPoint(c, 100, 30);
CGContextAddLineToPoint(c, 150, 70);
CGContextAddLineToPoint(c, 200, 50);
CGContextSetLineWidth(c, 10);
CGContextSetLineJoin(c, kCGLineJoinBevel);
CGContextSetLineCap(c, kCGLineCapRound);
CGContextSetLineDash(c, 0, lengths, 2);
CGContextSetRGBStrokeColor(c, 0, 0, 0, 1);
CGContextStrokePath(c);
CGContextTranslateCTM(c, 0, 100);
putstr(c, "Dash With CGPath Functions", 10, 10);
CGContextTranslateCTM(c, 0, 100);
putstr(c, "Dash + Stroke With CGPath Functions", 10, 10);
CGContextRestoreGState(c);
}
- (BOOL)isFlipped
{
return YES;
}
@end
@interface appDelegate : NSObject<NSApplicationDelegate>
@end
@implementation appDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)note
{
NSWindow *mainwin;
NSView *contentView;
dashStrokeView *view;
NSDictionary *views;
NSArray *constraints;
mainwin = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, 320, 240)
styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
backing:NSBackingStoreBuffered
defer:YES];
[mainwin setTitle:@"Dash/Stroke Example"];
contentView = [mainwin contentView];
view = [[dashStrokeView alloc] initWithFrame:NSZeroRect];
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
[contentView addSubview:view];
views = NSDictionaryOfVariableBindings(view);
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view]-|"
options:0
metrics:nil
views:views];
[contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[view]-|"
options:0
metrics:nil
views:views];
[contentView addConstraints:constraints];
[mainwin cascadeTopLeftFromPoint:NSMakePoint(20, 20)];
[mainwin makeKeyAndOrderFront:nil];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app
{
return YES;
}
@end
int main(void)
{
NSApplication *app;
app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
[app setDelegate:[appDelegate new]];
[app run];
return 0;
}
答案 0 :(得分:3)
问题是CGFloat O(1)
的值不够大,无法从它的路径中辨别出来。增加浮动值将使斜面和盖帽间隔更充分:
lengths
你现在获得它的方式实际上是绘制(不是还原),虽然它是如此微不足道,它本身重叠,基本上创造了一条实线。