Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect CAShapeLayer touch

Tags:

ios

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?

like image 439
user1333444 Avatar asked Apr 14 '12 16:04

user1333444


1 Answers

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;
                }
            }
        }
    }
}
like image 115
Jody Hagins Avatar answered Oct 07 '22 21:10

Jody Hagins