1

How I would go about capturing touch events on a CATiledLayer?

I have the following situation where if I change the view layer to a CATiledLayer the view stops capturing TouchesXXX events. Why does this happens and how should I address this issue.

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
    }
}
ademar
  • 774
  • 8
  • 16

1 Answers1

2

I really do not know what is the reason for this. However, I made it work with two different ways:

  1. By leaving the default type of the layer to CALayer, creating a new CATiledLayer and adding it as a sublayer to the view's Layer. I guess this is not a proper solution.

  2. By using a WeakDelegate, instead of a strongly typed Delegate. Place the following in your view subclass:

    [Export("drawLayer:inContext:")]

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

and change the following line

tiledLayer.Delegate = new TiledLayerDelegate (this);

to:

tiledLayer.WeakDelegate = this;
Dimitris Tavlikos
  • 8,170
  • 1
  • 27
  • 31