CATiledLayer,CALayerDelegate和捕获触摸事件

时间:2012-02-20 16:32:45

标签: ios xamarin.ios calayer catiledlayer

如何在CATiledLayer上捕获触摸事件?

我有以下情况,如果我将视图层更改为CATiledLayer,视图将停止捕获TouchesXXX事件。 为什么会发生这种情况,我应该如何解决这个问题。

public partial class DocumentView : UIView
{
    public DocumentView ()
    {
        //if the following lines are removed touch events are captured
        //otherwise they are not
        var tiledLayer = Layer as CATiledLayer;
        tiledLayer.Delegate = new TiledLayerDelegate (this);
    }

    public override void TouchesBegan (NSSet touches, UIEvent evt)
    {
        //does not get called if we hook TiledLayerDelegate up there in the construct  
        base.TouchesBegan (touches, evt);
    }
}

public class TiledLayerDelegate : CALayerDelegate
{
    public DocumentView DocumentView;

    public TiledLayerDelegate (DocumentView documentView)
    {
        DocumentView = documentView;
    }

    public override void DrawLayer (CALayer layer, CGContext context)
    {
        //draw happens here
    }
}

1 个答案:

答案 0 :(得分:2)

我真的不知道这是什么原因。但是,我用两种不同的方式使它工作:

  1. 将图层的默认类型保留为CALayer,创建新的CATiledLayer并将其作为子图层添加到视图的Layer。我想这不是一个合适的解决方案。

  2. 使用WeakDelegate代替强类型Delegate。将以下内容放在视图子类中:

    [导出( “drawLayer:inContext的:”)]

    public void DrawLayer(CALayer layer, CGContext context)
    {
        //draw happens here 
    }
    
  3. 并更改以下行

    tiledLayer.Delegate = new TiledLayerDelegate (this);
    

    为:

    tiledLayer.WeakDelegate = this;