Reputation: 187
I have a PNG in a UIImageView with alpha around the edges (let's say a circle). When I tap it, I want it to register as a tap for the circle if I'm touching the opaque bit, but a tap for the view behind if I touch the transparent bit.
(BTW: On another forum, someone said PNGs automatically do this, and a transparent PNG should pass the click on to the view below, but I've tested it and it doesn't, at least not in my case.)
Is there a flag I just haven't flipped, or do I need to create some kind of formula: "if tapped { get location; calculate distance from centre; if < r { touched circle } else { pass it on } }"?
-k.
Upvotes: 2
Views: 631
Reputation: 7
I figured it out...the PNG, bounding box transparency issue and being able to click through to another image behind:
var hitTestPoint1:Boolean = false; var myHitTest1:Boolean = false; var objects:Array;
clip.addEventListener(MouseEvent.MOUSE_DOWN, doHitTest); clip.addEventListener(MouseEvent.MOUSE_UP, stopDragging); clip.buttonMode = true; clip.mouseEnabled = true; clip.mouseChildren = true;
clip2.addEventListener(MouseEvent.MOUSE_DOWN, doHitTest); clip2.addEventListener(MouseEvent.MOUSE_UP, stopDragging); clip2.buttonMode = true; clip2.mouseEnabled = true; clip2.mouseChildren = true;
clip.rotation = 60;
function doHitTest(event:MouseEvent):void { objects = stage.getObjectsUnderPoint(new Point(event.stageX, event.stageY)); trace("Which one: " + event.target.name); trace("What's under point: " + objects); for(var i:int=0; i
function stopDragging(event:MouseEvent):void { event.target.stopDrag(); }
function realHitTest(object:DisplayObject, point:Point):Boolean { /* If we're already dealing with a BitmapData object then we just use the hitTest * method of that BitmapData. */ if(object is BitmapData) { return (object as BitmapData).hitTest(new Point(0,0), 0, object.globalToLocal(point)); } else {
/* First we check if the hitTestPoint method returns false. If it does, that
* means that we definitely do not have a hit, so we return false. But if this
* returns true, we still don't know 100% that we have a hit because it might
* be a transparent part of the image.
*/
if(!object.hitTestPoint(point.x, point.y, true))
{
return false;
}
else {
/* So now we make a new BitmapData object and draw the pixels of our object
* in there. Then we use the hitTest method of that BitmapData object to
* really find out of we have a hit or not.
*/
var bmapData:BitmapData = new BitmapData(object.width, object.height, true, 0x00000000);
bmapData.draw(object, new Matrix());
var returnVal:Boolean = bmapData.hitTest(new Point(0,0), 0, object.globalToLocal(point));
bmapData.dispose();
return returnVal;
}
}
}
Upvotes: -1
Reputation: 31280
I don't believe that PNGs automatically do this, but can't find any references that definitively say one way or the other.
Your radius calculation is probably simpler, but you could also manually check the alpha value of the touched pixel in your image to determine whether to count it as a hit. This code is targetted at OS X 10.5+, but with some minor modifications it should run on iPhone: Getting the pixel data from a CGImage object. Here is some related discussion on retrieving data from a UIImage: Getting data from an UIImage.
Upvotes: 2