在SpriteKit中创建虚线(OS X)

时间:2017-06-17 18:48:44

标签: objective-c macos sprite-kit

我正在尝试使用SpriteKit中的SKShapeNode在Objective C中为Mac OS X创建一个虚线,而不是在iOS中而不是在Swift中。我已经为此看了很多SO文章,并尝试将它拼凑在一起。其中很多都是Swift的主要问题。这是我到目前为止所提出的,但它没有显示任何内容,我不确定原因:

-(void) drawBarLineDivisions {
    ...
    NSBezierPath *path = [NSBezierPath bezierPath];
    CGPoint startPoint = CGPointMake(0, 250);
    CGPoint endPoint = CGPointMake(450, 250);
    [path moveToPoint:startPoint];
    [path lineToPoint:endPoint];
    CGFloat pattern[2] = {10.0, 10.0};

    CGPathRef dashedLine = CGPathCreateCopyByDashingPath ([self CGPathFromPath:path], nil, 0, pattern, 2);
    SKShapeNode *line = [SKShapeNode shapeNodeWithPath:dashedLine];

    CGPathRelease(dashedLine);

    int x = i/(double)measureDivisions*[self getGridArea]/[self getMeasuresDisplayed];

    [line setPosition:CGPointMake(x, 0)];
    [line setFillColor:fillColor];
    [line setStrokeColor:[NSColor clearColor]];
    [self addChild:line];
}

//helper method for converting NSBezierPath into CGPath
- (CGMutablePathRef)CGPathFromPath:(NSBezierPath *)path
{
    CGMutablePathRef cgPath = CGPathCreateMutable();
    NSInteger n = [path elementCount];

    for (NSInteger i = 0; i < n; i++) {
        NSPoint ps[3];
        switch ([path elementAtIndex:i associatedPoints:ps]) {
            case NSMoveToBezierPathElement: {
                CGPathMoveToPoint(cgPath, NULL, ps[0].x, ps[0].y);
                break;
            }
            case NSLineToBezierPathElement: {
                CGPathAddLineToPoint(cgPath, NULL, ps[0].x, ps[0].y);
                break;
            }
            case NSCurveToBezierPathElement: {
                CGPathAddCurveToPoint(cgPath, NULL, ps[0].x, ps[0].y, ps[1].x, ps[1].y, ps[2].x, ps[2].y);
                break;
            }
            case NSClosePathBezierPathElement: {
                CGPathCloseSubpath(cgPath);
                break;
            }
            default: NSAssert(0, @"Invalid NSBezierPathElement");
        }
    }
    return cgPath;
}

0 个答案:

没有答案