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
}
}
I really do not know what is the reason for this. However, I made it work with two different ways:
By leaving the default type of the layer to
CALayer, creating a newCATiledLayerand adding it as a sublayer to the view’sLayer. I guess this is not a proper solution.By using a
WeakDelegate, instead of a strongly typedDelegate. Place the following in your view subclass:[Export(“drawLayer:inContext:”)]
and change the following line
to: