0

インターネットで見た標準コードを使用すると、単一の`CAShapeLayerでのタッチを検出できません。

CGPoint p = [[touches anyObject] locationInView:self.view];

    CGPathRef path = ((CAShapeLayer *)[lettersArray objectAtIndex:1]).path;

    if(CGPathContainsPoint(path, nil, p, NO))
    {
        ((CAShapeLayer *)[lettersArray objectAtIndex:0]).position = p;
        NSLog(@"Touched");
    }

セグメントだけでなく、少なくともいくつかの領域が必要ですか?

4

3 に答える 3

1

バディはこのコードを試してみてください

CGPathRef originalPath = shapeView.path;  //The single-line path

//Use the values you use to draw the path onscreen,
//or use a width representing how far the user can touch
//for it to be recognized by the path.
//For example, for an error tolerance of 4px, use a width of 8px.
CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(originalPath, NULL, path.lineWidth, path.lineCapStyle, path.lineJoinStyle, path.miterLimit);
BOOL pathContainsPoint = CGPathContainsPoint(strokedPath, NULL, touchLocation, NO);

NSLog(pathContainsPoint ? @"Yes" : @"No");
于 2015-02-20T10:03:18.953 に答える
0

はい、パスには、ポイントがパス内にあることをテストするためにCGPathContainsPoint用の領域が必要です。おそらくやりたいことは、ポイントからラインまでの距離を見つけて、それが特定のしきい値内にあることをテストすることです。

その記事の式は、セグメントではなくまでの距離を示していることに注意してください。線上の最も近いポイントがセグメントの境界を超えている場合は、ポイントからセグメントの端点までの距離も計算する必要があります。

于 2013-03-15T00:42:58.227 に答える
0
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{{

for (UITouch *touch in touches) {
    CGPoint touchLocation = [touch locationInView:self.view];
    for (id sublayer in self.view.layer.sublayers) {
        BOOL touchInLayer = NO;
        if ([sublayer isKindOfClass:[CAShapeLayer class]]) {
            CAShapeLayer *shapeLayer = sublayer;
            if (CGPathContainsPoint(shapeLayer.path, 0, touchLocation, YES)) {
                // This touch is in this shape layer
                touchInLayer = YES;
            }
        } else {
            CALayer *layer = sublayer;
            if (CGRectContainsPoint(layer.frame, touchLocation)) {
                // Touch is in this rectangular layer
                touchInLayer = YES;
            }
        }
    }
}

}

于 2016-03-25T11:38:42.037 に答える