ios7 – 在touchesmoved中用sprite工具包画一条线

我想沿着touchesmoved收集的点在sprite工具包中画一条线.

什么是最有效的方式?我尝试了几次,我的线在y轴上是错误的,或者占用了很多处理能力,fps下降到10秒.

有任何想法吗?

解决方法

您可以定义CG路径并通过在触摸移动功能中添加线条或弧线来修改它.之后,您可以从路径创建SKShapeNode并根据需要进行配置.
如果要在手指在屏幕上移动时绘制线条,则可以在触摸以空路径开始时创建形状节点,然后对其进行修改.

编辑:我写了一些代码,它适用于我,绘制一个简单的红线.

在你的MyScene.m中:

@interface MyScene()
{
    CGMutablePathRef pathToDraw;
    SKShapeNode *lineNode;
}
@end

@implementation
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw,NULL,positionInScene.x,positionInScene.y);

    lineNode = [SKShapeNode node];
    lineNode.path = pathToDraw;
    lineNode.strokeColor = [SKColor redColor];
    [self addChild:lineNode];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    CGPathAddLineToPoint(pathToDraw,positionInScene.y);
    lineNode.path = pathToDraw;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// delete the following line if you want the line to remain on screen.
    [lineNode removeFromParent];
    CGPathRelease(pathToDraw);
}
@end

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...