gpt4 book ai didi

c# - CALayer initWithLayer : bound to default constructor by mistake? 我可以覆盖默认构造函数吗?

转载 作者:行者123 更新时间:2023-11-30 21:08:35 24 4
gpt4 key购买 nike

我无法为 CALayer 的 initWithLayer:(layer) 选择器找到任何我可以覆盖的等效绑定(bind)方法。

查看 Monotouch 程序集,选择器 initWithLayer 绑定(bind)到默认构造函数,如下所示:

[Export ("initWithLayer:")]
public CALayer (CALayer other)
{
if (base.GetType () == typeof(CALayer))
{
Messaging.intptr_objc_msgSend_intptr (base.Handle, CALayer.selInitWithLayer, other.Handle);
}
else
{
Messaging.intptr_objc_msgSendSuper_intptr (base.SuperHandle, CALayer.selInitWithLayer, other.Handle);
this.Clone (other);
}
}

但是我需要能够覆盖 initWithLayer 以便在我的 CALayer 类中为我的自定义属性设置动画。 (如我之前的问题中所述 Animate a custom property using CoreAnimation in Monotouch? )

这是错误完成的,还是我说不可能覆盖默认构造函数的说法不正确?

Apple 文档指出 initWithLayer 的唯一用途是创建卷影副本,而不是用于初始化层,这让我觉得它可能是一个 Monotouch 错误

Apple 文档

initWithLayer: Override to copy or initialize custom fields of the specified layer.

  • (id)initWithLayer:(id)layer Parameters layer The layer from which custom fields should be copied. Return Value A layer instance with any custom instance variables copied from layer.

Discussion This initializer is used to create shadow copies of layers, for example, for the presentationLayer method.

Subclasses can optionally copy their instance variables into the new object.

Subclasses should always invoke the superclass implementation

Note: Invoking this method in any other situation will produce undefined behavior. Do not use this method to initialize a new layer with an existing layer’s content. Availability Available in iOS 2.0 and later. Declared In CALayer.h

编辑:

我需要覆盖此方法的原因是 CoreAnimation 框架调用此方法来创建自己的图层内部副本。我需要能够覆盖它以包含我添加到图层的新属性,以便 CoreAnimation 知道在动画过程中补间我的属性值。我不会自己调用它。这就是为什么我不能简单地调用 Clone()

我已经尝试对 Rolf 的代码进行修改,但仍然没有被调用:

//Needed to do this as sel_registerName is private
[DllImport ("/usr/lib/libobjc.dylib")]
internal static extern IntPtr sel_registerName (string name);

static IntPtr selInitWithLayer = sel_registerName("initWithLayer:");

[Export ("initWithLayer:")]
public CircleLayer (CALayer other) //changed SuperHandle to other.SuperHandle as using this.SuperHandle gives me an object reference required error.
: base (Messaging.intptr_objc_msgSendSuper_intptr (other.SuperHandle, CircleLayer.selInitWithLayer, other.Handle))
{
// custom initialization here
Console.WriteLine("Got to here");
}

最佳答案

I cannot find any equivalent binded method for CALayer's initWithLayer:(layer) selector that I can override.

Looking at the Monotouch Assembly, the selector initWithLayer is bound to the default constructor like so:

一些术语:public CALayer (CALayer other) 不是默认构造函数——默认构造函数没有任何参数。

However I need to be able to override the initWithLayer in order to animate my custom property in my CALayer class. (As specified in my previous question Animate a custom property using CoreAnimation in Monotouch?)

这个问题并没有说明为什么需要重写 initWithLayer 构造函数。我确实找到了这个:Why animating custom CALayer properties causes other properties to be nil during animation? ,这似乎是你想要做的。在这种情况下,我认为有一种更简单的方法可以满足您的需求:您只需要覆盖自定义 CALayer 类中的 Clone 方法,并在那里进行适当的初始化。

Was this done by mistake, or am I incorrect in saying that it's not possible to override a default constructor?

从技术上讲,在 C# 中您不会覆盖构造函数。在您的基类中,您提供自己的构造函数,并且所有构造函数都必须调用直接基类中的一个构造函数(在某些情况下,这可能由编译器隐式完成)。

就是说,在 MonoTouch 中,您可以在您的类中提供一个构造函数来执行您想要的所有操作(不过我仍然认为您应该先尝试 Clone 方法):

public MyCALayer : CALayer {
[Export ("initWithLayer:")]
public MyCALayer (CALayer other) : base (other)
{
// custom initialization here
}

public override void Clone ()
{
// copy any code that you care about here.
}
}

关于c# - CALayer initWithLayer : bound to default constructor by mistake? 我可以覆盖默认构造函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9579532/

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