触及透明的PNG

时间:2009-05-18 15:48:58

标签: iphone objective-c cocoa-touch transparency

我在UIImageView中有一个PNG,边缘有alpha(让我们说一个圆圈)。当我点击它时,如果我触摸不透明位,我希望它注册为圆圈的点击,但如果我触摸透明位,则点击后面的视图。

(顺便说一句:在另一个论坛上,有人说PNG会自动执行此操作,并且透明的PNG应该将点击转到下面的视图,但我已经测试了它并没有,至少在我的情况下不是。 )

是否有一个我没有翻过的旗帜,或者我是否需要创建某种公式:“如果点击{获取位置;计算距离中心的距离;如果< r {触及圈子}否则{传递给它“?

-k。

2 个答案:

答案 0 :(得分:2)

我不相信PNG会自动执行此操作,但找不到任何明确说明这种或那种方式的参考文献。

您的半径计算可能更简单,但您也可以手动检查图像中触摸像素的Alpha值,以确定是否将其计为命中。此代码的目标是OS X 10.5+,但经过一些小修改后,它应该在iPhone上运行:Getting the pixel data from a CGImage object。以下是从UIImage检索数据的相关讨论:Getting data from an UIImage

答案 1 :(得分:-1)

我想通了...... PNG,边界框透明度问题,能够点击后面的另一张图片:

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(“哪一个:”+ event.target.name);     跟踪(“什么是重点:”+对象);     for(var i:int = 0; i

function stopDragging(event:MouseEvent):void {     event.target.stopDrag(); }

function realHitTest(object:DisplayObject,point:Point):Boolean {     / *如果我们已经处理了BitmapData对象,那么我们只使用hitTest      * BitmapData的方法。     * /     if(对象是BitmapData)     {         return(object as BitmapData).hitTest(new Point(0,0),0,object.globalToLocal(point));     }     别的{

    /* 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;
    }
}

}