gpt4 book ai didi

objective-c - 我无法填写我的 CGPath

转载 作者:太空狗 更新时间:2023-10-30 03:49:06 24 4
gpt4 key购买 nike

我需要一些帮助来填补我的道路。例如,我不知道将 CGContextFillPath(path); 放在哪里。我对绘图(和 iOS 开发)还很陌生,现在我真的尝试了将近两个小时的一切。但我到了不得不放弃的地步......希望你能阐明我的问题。我收到以下错误:无效上下文

-(CGMutablePathRef) drawHexagon:(CGPoint)origin
{
//create mutable path
CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, nil, origin.x, origin.y);

CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42);
CGPathMoveToPoint(path, NULL, newloc.x, newloc.y);
CGPathAddLineToPoint(path, NULL, newloc.x + 16,newloc.y + 38);
CGPathAddLineToPoint(path, NULL, newloc.x + 49, newloc.y + 0);
CGPathAddLineToPoint(path, NULL, newloc.x + 23, newloc.y - 39);
CGPathAddLineToPoint(path, NULL, newloc.x - 25,newloc.y - 40);
CGPathAddLineToPoint(path, NULL, newloc.x -43, newloc.y + 0);
CGPathCloseSubpath(path);
CGContextAddPath(context, path);
context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillPath(path);



return path;

}

-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {

clickedHexagonsUpToNow = [[NSMutableArray alloc]init ];
CGSize screenSize = [CCDirector sharedDirector].winSize;

background = [CCSprite spriteWithFile:@"background.png"];
background.anchorPoint= ccp(0,0);
[self addChild:background z:-1 tag:0];

hex1TouchArea = [self drawHexagon:ccp(82,120)];
hex2TouchArea = [self drawHexagon:ccp(50,157)];
hex3TouchArea = [self drawHexagon:ccp(220 ,196)];
hex4TouchArea = [self drawHexagon:ccp(280,153)];
hex5TouchArea = [self drawHexagon:ccp(84,118)];
hex6TouchArea = [self drawHexagon:ccp(82,120)];
hex7TouchArea = [self drawHexagon:ccp(82,120)];
hex8TouchArea = [self drawHexagon:ccp(82,120)];
hex9TouchArea = [self drawHexagon:ccp(82,120)];


self.isTouchEnabled = YES;

}
return self;
}

最佳答案

它应该以 CGContext 作为参数,而不是 CGPath。

CGContextFillPath(context);

但是,对 CGContextStrokePath 的调用清除了当前路径,因此 CGContextFillPath 将不执行任何操作。要在同一路径上描边和填充,您应该使用 CGContextDrawPath :

    ...
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextAddPath(context, path);
// CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
// CGContextFillPath(path);
CGContextDrawPath(context, kCGPathFillStroke);

(并且还应用 @Luiz 关于获取上下文的更改。)

关于objective-c - 我无法填写我的 CGPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8312933/

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