Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Something I stumbled across while scratching my head over a similar problem is that CALayer uses <code>containsPoint:</code> when you send it <code>hitTest:</code></p> <p>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:</p> <pre><code>- (BOOL) containsPoint:(CGPoint)p { return CGRectContainsPoint(self.bounds, p) &amp;&amp; !ImagePointIsTransparent(self.contents, p)) return YES; } </code></pre> <p>There's a discussion of testing for a single pixel's alpha at <a href="https://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage">Retrieving a pixel alpha value for a UIImage</a></p> <p>This worked for my purposes:</p> <pre><code>static BOOL ImagePointIsTransparent(CGImageRef image, CGPoint p) { uint8_t alpha; CGContextRef context = CGBitmapContextCreate(&amp;alpha, 1, 1, 8, 1, NULL, kCGImageAlphaOnly); CGContextDrawImage(context, CGRectMake(-p.x, -p.y, CGImageGetWidth(image), CGImageGetHeight(image)), image); CGContextRelease(context); return alpha == 0; } </code></pre> <p>(If you're using <code>renderInContext:</code> 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: <a href="http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/" rel="nofollow noreferrer">http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/</a>)</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COChris, 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.
      singulars
    2. COAs 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.
      singulars
    3. COChris, 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.) http://stackoverflow.com/questions/7506248/alpha-detection-in-layer-ok-on-simulator-not-iphone
      singulars
 

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