Hit Testing with CALayer using the alpha properties of the CALayer contents
NickName:Charliehorse Ask DateTime:2010-05-31T22:10:08

Hit Testing with CALayer using the alpha properties of the CALayer contents

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?

Copyright Notice:Content Author:「Charliehorse」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2944064/hit-testing-with-calayer-using-the-alpha-properties-of-the-calayer-contents

More about “Hit Testing with CALayer using the alpha properties of the CALayer contents” related questions

Hit Testing with CALayer using the alpha properties of the CALayer contents

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

Show Detail

Using a CALayer to display an alpha mapped image

I have a CALayer object into which I would like to insert a UIImage equipped with an alpha map, simply by setting the layer's contents property. However, when I display this CALayer, all transparen...

Show Detail

CALayer alpha mask not working

I'm trying to create a simple example of drawing an image to a layer, and then setting that layer's alpha mask. I added the code below to my sample app's viewDidLoad. without setting the mask, i ...

Show Detail

CALayer not rendering contents

This one is stumping me. I have a CALayer which I create in a UIViewController's viewDidLoad method something like this: - (void)viewDidLoad { [super viewDidLoad]; UIImage* img = [UIImage

Show Detail

hitTest on CALayer - how do you find which actual layer was hit?

Situation: need to find which layer the user has touched. Problem: Apple says we should use [CALayer presentationLayer] to do hit-testing, so that it represents what is actually on screen at the t...

Show Detail

How to add color over CALayer() with alpha value?

I have working slideshow codes with CALayer() and I want to add Brown color mask over images with alpha value. My CALayer() codes under below. let imageLayer = CALayer() imageLayer.contents =

Show Detail

CALayer: Maintaining cornerRadius on layer's contents

I've been looking at the CALayer's documentation and it seems like cornerRadius only affects the background of the layer and not the contents. Is there a way to apply the corner radius to the entire

Show Detail

Mask CALayer with Another CALayer

I'm trying to mask one CALayer(newSticker) with the image of another layer(stickerMask), like so: func placeSticker(location: CGPoint) { let stickerMask = CALayer() stickerMask.conten...

Show Detail

Why does CALayer not maintain its contents?

I'm using CALayers to display a couple images. I do so by creating the layer and setting its contents property to a CGImageRef. I do not set a delegate on my CALayer. The layer displays fine, but ...

Show Detail

Set contents of CALayer to animated GIF?

Is it possible to set the contents of a CALayer to an animated GIF and have it display that animation? I know that I can set the contents to an image like so: CALayer* subLayer = [CALayer layer];

Show Detail