Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Spritekit-使用手势同时用多个手指画多条线_Ios_Objective C_Sprite Kit - Fatal编程技术网

Ios Spritekit-使用手势同时用多个手指画多条线

Ios Spritekit-使用手势同时用多个手指画多条线,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,使用下面的代码,我可以用一个手指用平移手势画一条线。然而,我试图做的是让一个手指画一条线,另一个手指同时画第二条线。我在某个地方读过关于使用字典存储触摸的文章,但不知道如何用代码编写。有人能帮忙吗 -(void)didMoveToView:(SKView *)view { UIPanGestureRecognizer *gestureRecognizerPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@s

使用下面的代码,我可以用一个手指用平移手势画一条线。然而,我试图做的是让一个手指画一条线,另一个手指同时画第二条线。我在某个地方读过关于使用字典存储触摸的文章,但不知道如何用代码编写。有人能帮忙吗

-(void)didMoveToView:(SKView *)view {

    UIPanGestureRecognizer *gestureRecognizerPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
    [[self view] addGestureRecognizer:gestureRecognizerPan];

}

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {

        CGPoint touchLocation = [recognizer locationInView:recognizer.view];
        touchLocation = [self convertPointFromView:touchLocation];

        pathToDraw = CGPathCreateMutable();
        CGPathMoveToPoint(pathToDraw, NULL, touchLocation.x, touchLocation.y);
        lineNode = [[SKShapeNode alloc] init];
        lineNode.path = pathToDraw;
        lineNode.name = lineNodeCategoryName;
        [_gameLineNode addChild:lineNode];

    } else if (recognizer.state == UIGestureRecognizerStateChanged) {

        CGPoint touchLocation = [recognizer locationInView:recognizer.view];
        touchLocation = [self convertPointFromView:touchLocation];

        CGPathAddLineToPoint(pathToDraw, NULL, touchLocation.x, touchLocation.y);
        lineNode.path = pathToDraw;
        lineNode.name = lineNodeCategoryName;
        lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:pathToDraw];
        lineNode.physicsBody.dynamic = YES;
        lineNode.strokeColor = [SKColor blackColor];
        lineNode.glowWidth = 3.0;

    } else if (recognizer.state == UIGestureRecognizerStateEnded) {

        CGPathRelease(pathToDraw);

    }
}

同时绘制多条线相当简单。关键是独立跟踪每个触摸事件。一种方法是维护一个字典,使用触摸事件作为键,形状节点(用于绘制线)作为值

@implementation GameScene {
    NSMutableDictionary *lines;
}

-(void)didMoveToView:(SKView *)view {
    self.scaleMode = SKSceneScaleModeResizeFill;
    // Create a mutable dictionary
    lines = [NSMutableDictionary dictionaryWithCapacity:10];
}

// Add each line node to the dictionary with the touch event as the key
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        // Create a mutable path
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:location];
        // Create a shape node using the path
        SKShapeNode *lineNode = [SKShapeNode shapeNodeWithPath:path.CGPath];
        lineNode.strokeColor = [SKColor blackColor];
        [self addChild:lineNode];
        // Use touch pointer as the dictionary key. Since the dictionary key must conform to
        // NSCopying, box the touch pointer in an NSValue
        NSValue *key = [NSValue valueWithPointer:(void *)touch];
        [lines setObject:lineNode forKey:key];
    }
}

// Update the lines as needed
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        // Retrieve the shape node that corresponds to touch
        NSValue *key = [NSValue valueWithPointer:(void *)touch];
        SKShapeNode *lineNode = [lines objectForKey:key];
        if (lineNode != NULL) {
            // Create and initialize a mutable path with the lineNode's current path
            UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:lineNode.path];
            // Add a line to the current touch point
            [path addLineToPoint:location];
            // Update lineNode
            lineNode.path = path.CGPath;
        }
    }
}

// Remove the line nodes from the dictionary when the touch ends
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        NSValue *key = [NSValue valueWithPointer:(void *)touch];
        [lines removeObjectForKey:key];
    }
}

@end

伙计,这太棒了。他很有魅力。非常感谢。多重线条图让我可以创建很多额外的层次。我想,当其他人做类似的动作时,也可以使用同样的技术。我尽量避免在SK中使用手势识别器,因为当你转换到另一个场景时,需要手动删除它们。我相信,以上是一种更加友好的方法。嗨,快速提问。不久前你帮过我,如果你能帮忙的话,需要更多的指导。我们使用字典允许画多行。我没有包括结束代码删除行,但我想删除通过SKButton一行一行。你能告诉我,我可以用什么方法一条一条地删除这些行吗?i、 e同时画两条线,松开并画下一条线。单击删除图标,首先删除单行。再次单击并删除前两行。谢谢。@user3482617我建议您在创建线时将线(即形状节点)添加到数组中,然后在点击按钮时从数组中检索适当的线。然后,可以从场景和阵列中删除线。