私はこのコードで線を引いています:
- (void) drawLine:(float)x :(float)y:(float)toX:(float)toY
{
CAShapeLayer *lineShape = nil;
CGMutablePathRef linePath = nil;
linePath = CGPathCreateMutable();
lineShape = [CAShapeLayer layer];
lineShape.lineWidth = 1.0f;
lineShape.lineCap = kCALineJoinMiter;
lineShape.strokeColor = [[UIColor redColor] CGColor];
CGPathMoveToPoint(linePath, NULL, x, y);
CGPathAddLineToPoint(linePath, NULL, toX, toY);
lineShape.path = linePath;
CGPathRelease(linePath);
if(x != 0 && y != 0)
[myView.layer addSublayer:lineShape];
}
今、私は私のラインがいつ接触するか知りたいです。どうしてそれは可能ですか?使ってます
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSIndexSet *indexSet = [myView.layer.sublayers indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
return [obj isMemberOfClass:[CAShapeLayer class]];
}];
NSArray *textLayers = [myView.layer.sublayers objectsAtIndexes:indexSet];
for (CAShapeLayer *textLayer in textLayers) {
CGPoint p = [[touches anyObject] locationInView:myView];
NSLog(@"touch x is :%f",p.x);
CGAffineTransform transf = CGAffineTransformMakeTranslation(-textLayer.position.x, - textLayer.position.y);
if(CGPathContainsPoint(textLayer.path, &transf, p, NO)){
NSLog(@"touched..");
}
}
}しかし、CGPathContainsPointメソッドでは、タッチがラインパスに属しているかどうかがわかりません。