【问题标题】:How to make a CGPath without Fill?如何制作没有填充的CGPath?
【发布时间】:2014-08-26 14:02:55
【问题描述】:

我正在尝试制作一个没有填充的 SKShapeNode,但我找不到仅使用我绘制的线条制作 cgpath 的方法,我通过添加填充颜色并尝试使用其他 cgpath 参数来查看是否它没有填充,但它可以工作,这里的目标是只使用用户绘制的上下文创建一个 skshapenode,然后将一个物理体分配给用户绘制的线条或形状。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   touch1 = [touches anyObject];
   location1 = [touch1 locationInNode:self];

    pathTodraw = CGPathCreateMutable();

    CGPathMoveToPoint(pathTodraw, nil, location1.x, location1.y);



    sprite = [SKShapeNode node];

    sprite.path = pathTodraw;


    NSArray *colors = @[[UIColor blackColor],[UIColor redColor],[UIColor cyanColor]];
    NSUInteger colorindex = arc4random() %colors.count;

    sprite.strokeColor =[UIColor blueColor];
    sprite.lineWidth = 3.0f;
    sprite.fillColor = [colors objectAtIndex:colorindex];


    [self addChild:sprite];




}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

   touch2 = [touches anyObject];

    location2 = [touch2 locationInNode:self];

    CGPathAddLineToPoint(pathTodraw, nil, location2.x, location2.y);





    sprite.path = pathTodraw;


}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    sprite.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:pathTodraw];
    sprite.physicsBody.dynamic = YES;
    sprite.physicsBody.affectedByGravity = YES;

}
-(void)update:(CFTimeInterval)currentTime {

    if([self children].count>5){

    SKAction *remove = [SKAction sequence:@[[SKAction waitForDuration:2.0],[SKAction fadeAlphaTo:0 duration:1.0],[SKAction removeFromParent]]];

    [sprite runAction:remove];

        [self removeAllChildren];




    }
}

【问题讨论】:

  • 当您不想让 SKShapeNode 填充路径时,为什么要将填充颜色设置为不透明的颜色?

标签: ios objective-c core-graphics sprite-kit cgpath


【解决方案1】:

如果SKShapeNode 将您提供的路径视为要填充的内容,那么您需要提供一个路径,在填充时生成您想要的行。这正是CGPathCreateCopyByStrokingPath() 所做的。它接受一个路径和一组画线控制参数,确定您通过使用这些参数描边该路径所获得的形状,并创建一个新路径,该路径在填充时会产生相同的形状。

【讨论】:

    猜你喜欢
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2023-03-27
    相关资源
    最近更新 更多