I have created a spider chart by overiding draw rect, I am using core grahics CAShapeLayer to draw my areas, there are multiple CAShapeLayer regions which are created on the screen, I want to detect which layer is touched when the users touches... but I can't figure out how?
First, you should not be drawing layers in drawRect, but that is not your question. To identify a layer that is "touched" you can do something like this...
- (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;
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With