我将如何在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
    }
}

最佳答案

我真的不知道是什么原因。但是,我通过两种不同的方式使其工作:

  • 通过将层的默认类型保留为CALayer,创建新的CATiledLayer并将其作为子层添加到视图的Layer中。我猜这不是一个适当的解决方案。
  • 通过使用WeakDelegate代替强类型的Delegate。将以下内容放在您的视图子类中:

    [导出(“drawLayer:inContext:”)]
    public void DrawLayer(CALayer layer, CGContext context)
    {
        //draw happens here
    }
    

  • 并更改以下行
    tiledLayer.Delegate = new TiledLayerDelegate (this);
    

    至:
    tiledLayer.WeakDelegate = this;
    

    关于ios - CATiledLayer,CALayerDelegate和捕获触摸事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9364670/

    10-09 16:26