Ios 如何在SpriteKit中高效地绘制直线
在我的SpriteKit场景中,用户应该能够用手指画线。我有一个可行的解决方案,如果线很长,FPS下降到4-6,线开始变成多边形,如下图所示: 为了绘制Ios 如何在SpriteKit中高效地绘制直线,ios,objective-c,ios7,sprite-kit,line-drawing,Ios,Objective C,Ios7,Sprite Kit,Line Drawing,在我的SpriteKit场景中,用户应该能够用手指画线。我有一个可行的解决方案,如果线很长,FPS下降到4-6,线开始变成多边形,如下图所示: 为了绘制myline(SKShapeNode*),我以这种方式收集NSMutableArray*noteLinePoints中的触摸移动点 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint touchPoint = [[touches anyObj
myline
(SKShapeNode*
),我以这种方式收集NSMutableArray*noteLinePoints
中的触摸移动点
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInNode:self.scene];
SKNode *node = [self nodeAtPoint:touchPoint];
if(noteWritingActive)
{
[noteLinePoints removeAllObjects];
touchPoint = [[touches anyObject] locationInNode:_background];
[noteLinePoints addObject:[NSValue valueWithCGPoint:touchPoint]];
myline = (SKShapeNode*)node;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(myline)
{
CGPoint touchPoint = [[touches anyObject] locationInNode:_background];
[noteLinePoints addObject:[NSValue valueWithCGPoint:touchPoint]];
[self drawCurrentNoteLine];
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if(myline)
{
myline.name = @"note";
myline = nil;
}
NSLog(@"touch ended");
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event
{
CGPoint touchPoint=[[touchs anyObject]locationInNode:self.scene];
SKNode*node=[自身节点点:接触点];
如果(noteWritingActive)
{
[注:线条点删除所有对象];
touchPoint=[[Touchs anyObject]位置Innode:_background];
[noteLinePoints添加对象:[NSValue valueWithCGPoint:touchPoint]];
myline=(SKShapeNode*)节点;
}
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
if(myline)
{
CGPoint touchPoint=[[touchs anyObject]locationInNode:_background];
[noteLinePoints添加对象:[NSValue valueWithCGPoint:touchPoint]];
[self drawCurrentNoteLine];
}
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event
{
if(myline)
{
myline.name=@“注意”;
myline=nil;
}
NSLog(@“触摸结束”);
}
我是这样画的
- (CGPathRef)createPathOfCurrentNoteLine
{
CGMutablePathRef ref = CGPathCreateMutable();
for(int i = 0; i < [noteLinePoints count]; ++i)
{
CGPoint p = [noteLinePoints[i] CGPointValue];
if(i == 0)
{
CGPathMoveToPoint(ref, NULL, p.x, p.y);
}
else
{
CGPathAddLineToPoint(ref, NULL, p.x, p.y);
}
}
return ref;
}
- (void)drawCurrentNoteLine
{
if(myline)
{
SKNode* oldLine = [self childNodeWithName:@"line"];
if(oldLine)
[self removeChildrenInArray:[NSArray arrayWithObject:oldLine]];
myline = nil;
myline = [SKShapeNode node];
myline.name = @"line";
[myline setStrokeColor:[SKColor grayColor]];
CGPathRef path = [self createPathOfCurrentNoteLine];
myline.path = path;
CGPathRelease(path);
[_background addChild:myline];
}
}
-(CGPathRef)CreatePathFCurrentNoteline
{
CGMutablePathRef=CGPathCreateMutable();
对于(int i=0;i<[noteLinePoints count];++i)
{
CGPoint=[noteLinePoints[i]CGPointValue];
如果(i==0)
{
CGPathMoveToPoint(参考,空,p.x,p.y);
}
其他的
{
CGPathAddLineToPoint(参考,空,p.x,p.y);
}
}
返回ref;
}
-(void)drawCurrentNoteLine
{
if(myline)
{
SKNode*oldLine=[self childNodeWithName:@“line”];
如果(旧线)
[self-removeChildrenarray:[NSArray arrayWithObject:oldLine]];
myline=nil;
myline=[SKShapeNode];
myline.name=@“行”;
[myline setStrokeColor:[SKColor grayColor]];
CGPathRef路径=[self CreatePathFCurrentNoteline];
myline.path=路径;
CGPathRelease(路径);
[_backgroundaddchild:myline];
}
}
我如何解决这个问题?因为所有后续的线条都是多边形的,我认为因为fps非常低,触摸的采样率也会自动变得非常低
请注意,在测试中,我使用的是iPad3(我的应用程序需要使用iOS7的iPad2型号)不要不断创建新的
SKShapeNodes
,有一个bug会导致你的速度减慢。相反,只使用1SKShapeNode
(或者创建一个束,但重复使用它们),并用新信息附加路径(因此无需不断将myline添加到背景)
备选方案:
使用1社区
SKSkapeNode
渲染路径,然后将SKShapeNode
转换为具有视图的纹理。textureFromNode
,然后使用此新纹理而不是形状节点添加SKSpriteNode
,不要不断创建新的SKShapeNode
,有一个bug导致您的速度减慢。相反,只使用1SKShapeNode
(或者创建一个束,但重复使用它们),并用新信息附加路径(因此无需不断将myline添加到背景)
备选方案:
使用1个社区
SKSkapeNode
渲染路径,然后将SKShapeNode
转换为具有视图的纹理。textureFromNode
,然后使用此新纹理而不是形状节点添加SKSpriteNode我的用户应该能够绘制他/她想要的任意多的线条,无论如何,我会尝试重复使用相同的SKShapeNode。谢谢你的快速回答!NP,也许他们会在iOS中修复SKShapeNodes10@ddb我发布了一个可能对你更有利的备选答案。好吧,我忘了在我的问题中提到我的应用程序需要在iOS7的iPad2机型上运行。。。我不是很幸运…这是一个商业应用程序,他们不会这么做。我的用户应该能够画出他/她想要的线,无论如何,我会尝试重复使用相同的SKShapeNode。谢谢你的快速回答!NP,也许他们会在iOS中修复SKShapeNodes10@ddb我发布了一个可能对你更有利的备选答案。好吧,我忘了在我的问题中提到我的应用程序需要在iOS7的iPad2机型上运行。。。我不是很幸运…这是一个商业应用,他们不会这么做