0

假设我制作了一个包含另一个 UIImageView 的 UIImageView。但是 SubView 比第一个视图大。

在此处输入图像描述

绿色子视图明显大于它的超级视图,黑色方块。如果我触摸只有黑色方块的左上角,我会得到“touchbegan image1”,这是我所期望的。如果我触摸黑色方块和绿色方块重叠的区域,我会得到“touchbegan image1”和“touchbegan image2”,这是我所期望的。 这是我的问题: 如果我触摸只有绿色方块存在的黑色方块框架之外的区域,我将没有输出。因此,既不为 image1 也不为 image2 调用 TouchesBegan。在这种情况下,当我仅触摸绿色矩形时,我希望为 image2 (Green) 调用 TouchesBegan,同时仍将 image2 (Green) 保持为 image1 (Black) 的子视图。那可能吗?

    MyImage image1 = new MyImage(new RectangleF(0,0,200,200), "image1");
    image1.BackgroundColor = UIColor.Black;
    this.View.AddSubview(image1);

    MyImage image2 = new MyImage(new RectangleF(50,50,500,500), "image2");
    image2.BackgroundColor = UIColor.Green;
    image1.AddSubview(image2);




public class MyImage : UIImageView
{

    public MyImage ( RectangleF frame, string _Name ) : base(frame)
    {
        Name = _Name;
        UserInteractionEnabled = true;
    }


    public override void TouchesBegan (NSSet touches, UIEvent evt)
    {
        base.TouchesBegan (touches, evt);

        Console.WriteLine("touchbegan " + Name);
    }
4

1 回答 1

0

Its because you add it as a child of image1. Image1 listens for touches inside its bound/frame and send it to its child if any. image2, might be bigger, but its touchlistener comes from image1 frame touch.

于 2013-02-09T10:26:52.453 回答