1

我在屏幕底部有一个点。. . 当我在某处触摸屏幕时,我希望在该点和我的手指所在的点之间出现一条虚线。线条的长度和旋转将根据我手指的位置或移动到的位置而改变。

我假设我会用重复的小线条图像制作虚线,但我想这就是我需要你帮助的原因!

4

1 回答 1

2

请注意,所有这些都可以更好地组织,我个人不喜欢任何形状的 SKShapeNode :) 或形式,但这是一种方法:

#import "GameScene.h"



@implementation GameScene{
    SKShapeNode *line;
}

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */

    line = [SKShapeNode node];
    [self addChild:line];
    [line setStrokeColor:[UIColor redColor]];

}

-(void)drawLine:(CGPoint)endingPoint{

    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
    CGPathAddLineToPoint(pathToDraw, NULL, endingPoint.x,endingPoint.y);

    CGFloat pattern[2];
    pattern[0] = 20.0;
    pattern[1] = 20.0;
    CGPathRef dashed =
    CGPathCreateCopyByDashingPath(pathToDraw,NULL,0,pattern,2);

    line.path = dashed;

    CGPathRelease(dashed);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        [self drawLine:location];

    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        [self drawLine:location];

    }
}

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

    line.path = nil;

}

结果是:

绘制虚线图像

此外,我不知道它的性能如何,但您可以对其进行测试、调整和改进。甚至像你说的那样使用 SKSpriteNode 。快乐编码!

编辑

我刚刚注意到你说的是虚线(不是虚线):)

您必须将模式更改为:

 pattern[0] = 3.0;
 pattern[1] = 3.0;
于 2015-03-31T01:32:44.840 回答