原來的文檔CGPathCreateCopyByDashingPath()
是錯誤的。
眼下,它說
創建新的路徑,以便加註新路徑得出相同的像素與指定的儀表參數撫摸着原始路徑。
這意味着它會產生具有默認筆畫參數的合成路徑。但它不!相反,你會得到一個新的路徑,這個路徑就是現有的由破折號參數分解的路徑。您需要撥打CGPathCreateCopyByStrokingPath()
才能生成填寫路徑。
以下程序有三個部分。首先通過使用CGContext
函數而不是CGPath
函數來顯示路徑應該看起來像什麼。其次,它只繪製CGPathCreateCopyByDashingPath()
。請注意,如何撫摸路徑不會產生破折號曾經是一堆盒子,而是一堆破折號。如果仔細觀察,你會發現線條連接處的藍色小病。最後,它調用CGPathCreateCopyByDashingPath()
,然後是CGPathCreateCopyByStrokingPath()
,你會看到填充即產生正確的輸出。再次
謝謝,bhaller!我不確定文件的變更內容,或者如何請求這樣的變更。
// 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] = { 10, 13 };
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);
// and reset
CGContextSetLineWidth(c, 1);
CGContextSetLineJoin(c, kCGLineJoinMiter);
CGContextSetLineCap(c, kCGLineCapButt);
CGContextSetLineDash(c, 0, NULL, 0);
CGContextTranslateCTM(c, 0, 100);
putstr(c, "Dash With CGPath Functions", 10, 10);
buildpath = CGPathCreateMutable();
CGPathMoveToPoint(buildpath, NULL, 50, 50);
CGPathAddLineToPoint(buildpath, NULL, 100, 30);
CGPathAddLineToPoint(buildpath, NULL, 150, 70);
CGPathAddLineToPoint(buildpath, NULL, 200, 50);
copy = CGPathCreateCopyByDashingPath(buildpath, NULL, 0, lengths, 2);
CGContextAddPath(c, copy);
CGContextStrokePath(c);
CGContextAddPath(c, copy);
CGContextSetRGBFillColor(c, 0, 0.25, 0.5, 1);
CGContextFillPath(c);
CGPathRelease(copy);
CGPathRelease((CGPathRef) buildpath);
CGContextTranslateCTM(c, 0, 100);
putstr(c, "Dash + Stroke With CGPath Functions", 10, 10);
buildpath = CGPathCreateMutable();
CGPathMoveToPoint(buildpath, NULL, 50, 50);
CGPathAddLineToPoint(buildpath, NULL, 100, 30);
CGPathAddLineToPoint(buildpath, NULL, 150, 70);
CGPathAddLineToPoint(buildpath, NULL, 200, 50);
copy = CGPathCreateCopyByDashingPath(buildpath, NULL, 0, lengths, 2);
copy2 = CGPathCreateCopyByStrokingPath(copy, NULL, 10, kCGLineCapRound, kCGLineJoinBevel, 10);
CGContextAddPath(c, copy2);
CGContextSetRGBFillColor(c, 0, 0.25, 0.5, 1);
CGContextFillPath(c);
CGContextAddPath(c, copy2);
CGContextStrokePath(c);
CGPathRelease(copy2);
CGPathRelease(copy);
CGPathRelease((CGPathRef) buildpath);
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, 360)
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;
}
您可以隨時向蘋果公司提交雷達(錯誤報告)以請求文檔更改:http://radar.apple.com/ –