7

I'm writing a game for Mac using Cocoa. I'm currently implementing hit testing and have founds that CALayer offers hit testing, but does not seem to implement the alpha properties. As I have at times many CALayers stacked on top of each other, I really need to find a way to determine what the user actually meant to click on.

I'm thinking if I could somehow get an array that contains pointers to all of the CALayers that contain the click point, I could filter through them some how. However the only way I've got so far to create the array is:

NSMutableArray* anArrayOfLayers = [NSMutableArray array];
    for (CALayer* aLayer in mapLayer.sublayers)
    {
        if ([aLayer containsPoint:mouseCoord])
            [anArrayOfLayers addObject:aLayer];
    }

Then sort the array by the CALayer's z-values then go through checking if the pixel at location is alpha or not. However, between the sort and the alpha check this seems to be an incredible performance hog. (How would you even check the alpha?)

Is there any way to do this?

1 Answer 1

10

Something I stumbled across while scratching my head over a similar problem is that CALayer uses containsPoint: when you send it hitTest:

Its default behaviour is to test against bounds, but we can override and get it to check the alpha channel, and just use CALayer's built in hit-testing to handle the rest:

- (BOOL) containsPoint:(CGPoint)p 
{
    return CGRectContainsPoint(self.bounds, p) && !ImagePointIsTransparent(self.contents, p)) return YES;
}

There's a discussion of testing for a single pixel's alpha at Retrieving a pixel alpha value for a UIImage

This worked for my purposes:

static BOOL ImagePointIsTransparent(CGImageRef image, CGPoint p)
{
    uint8_t alpha;

    CGContextRef context = CGBitmapContextCreate(&alpha, 1, 1, 8, 1, NULL, kCGImageAlphaOnly);
    CGContextDrawImage(context, CGRectMake(-p.x, -p.y, CGImageGetWidth(image), CGImageGetHeight(image)), image);
    CGContextRelease(context);

    return alpha == 0;
}

(If you're using renderInContext: to draw to the CALayer rather than setting its contents property, then it's going to be more complicated. This might be useful in that case: http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/)

6
  • Chris, this is terrific! Question for you: When passing in the point, it would seem to require its origin be at the lower-left vs. upper-right. I suspect that's something that must happen before passing the point in here, right? (Apart from using the image height to flip y around.) I guess I'm trying to determine where the correct place to put that adjustment is - in ImagePointIsTransparent, or in the gesture/touch handler. Sep 19, 2011 at 18:50
  • As it happens, I think the question I linked to was about what you're asking -- Core Graphics and UIKit have different Y axis, so I guess you'll want to convert at the interface between the two. There's also a discussion about the performance impact of different ways of rendering the layer (rendering all of it once vs just a pixel repeatedly), which you might also want to check out. Sep 19, 2011 at 21:14
  • Chris, I just realized: You're writing a Mac app. I'm writing an iOS app. It turns out that, when using a Retina display, this test does not work (on the iPhone 4 or the Simulator in Retina mode). The Quartz 2D Programming Guide recommends I use UIGraphicsBeginImageContextWithOptions vs. the lower-level Quartz functions, so I'll have to see if that helps with the CTM and so on. (This could also explain why the response seems "all over the place" on Retina.) stackoverflow.com/questions/7506248/… Sep 22, 2011 at 16:16
  • This is a perfect implementation for testing the alpha. I just wanted to mention that if an image is resized inside a layer. The hit test occurs on the original image location, not the resized location. There is most likely a way to convert the coordinates from the resized layer to the original image, but I have not found it as I don't need it. It hung me up for half a day, hopefully this will help someone else.
    – atreat
    Feb 17, 2012 at 19:54
  • 1
    Though I'm late to this party, I just wanted to say that I've made a sample app available for download with all of the necessary code required for pixel-level hit testing of CALayers as well as CAShapeLayers (that don't renderInContext:). It also has all that is needed for efficient testing or rect regions (think selection). The demo is available here Sep 30, 2012 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.