Note that there are some explanatory texts on larger screens.

plurals
  1. POhitTest overlapping CALayers
    primarykey
    data
    text
    <p>I have a UIView that contains a drawing that I've made using CALayers added as sublayers. It is a red square with a blue triangle centered inside. I am able to determine which shape has been touched using the following code:</p> <pre><code>CGPoint location = [gesture locationInView:self.view]; CALayer* layerThatWasTapped = [self.view.layer hitTest:location]; NSLog(@"Master Tap Location: %@", NSStringFromCGPoint(location)); NSLog(@"Tapped Layer Name: %@", layerThatWasTapped.name); NSLog(@"Tapped Layer Parent: %@", layerThatWasTapped.superlayer.name); int counter = layerThatWasTapped.superlayer.sublayers.count; NSArray * subs = layerThatWasTapped.superlayer.sublayers; //Loop through all sublayers of the picture for (int i=0; i&lt;counter; i++) { CALayer *layer = [subs objectAtIndex:i]; CAShapeLayer* loopLayer = (CAShapeLayer*)layerThatWasTapped.modelLayer; CGPathRef loopPath = loopLayer.path; CGPoint loopLoc = [gesture locationInView:cPage]; loopLoc = [self.view.layer convertPoint:loopLoc toLayer:layer]; NSLog(@"loopLoc Tap Location: %@", NSStringFromCGPoint(loopLoc)); //determine if hit is on a layer if (CGPathContainsPoint(loopPath, NULL, loopLoc, YES)) { NSLog(@"Layer %i Name: %@ Hit",i, layer.name); } else { NSLog(@"Layer %i Name: %@ No Hit",i, layer.name); } } </code></pre> <p>My problem lies with areas where the bounds of the triangle overlap the square. This results in the triangle registering the hit even when the hit is outside of the triangles path. This is a simplified example (I may have many overlapping shapes stacked in the view) Is there a way to loop through all of the sublayers and hittest each one to see if it lies under the tapped point? OR Is there a way to have the bounds of my layers match their paths so the hit occurs only on a visible area?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload