gpt4 book ai didi

ios - CATiledLayer、CALayerDelegate 和捕获触摸事件

转载 作者:行者123 更新时间:2023-11-29 11:16:10 27 4
gpt4 key购买 nike

我将如何捕获 CATiledLayer 上的触摸事件?

我遇到以下情况,如果我将 View 层更改为 CATiledLayer, View 将停止捕获 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. 通过将层的默认类型保留为 CALayer,创建一个新的 CATiledLayer 并将其作为子层添加到 View 的 Layer。我想这不是一个合适的解决方案。

  2. 通过使用 WeakDelegate,而不是强类型的 Delegate。将以下内容放入您的 View 子类中:

    [导出("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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com