【发布时间】:2012-03-06 07:26:21
【问题描述】:
我找不到任何可以覆盖的 CALayer 的 initWithLayer:(layer) 选择器的等效绑定方法。
查看 Monotouch 程序集,选择器 initWithLayer 绑定到默认构造函数,如下所示:
[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?中指定)
这是错误地完成,还是我说不可能覆盖默认构造函数是不正确的?
Apples 文档指出 initWithLayer 的唯一用途是创建卷影副本,而不是用于初始化图层,这让我觉得它可能是 Monotouch 错误
Apple 文档
initWithLayer:覆盖以复制或初始化自定义字段 指定层。
- (id)initWithLayer:(id)layer 参数 layer 应该从中复制自定义字段的层。返回值 具有任意 从图层复制的自定义实例变量。
讨论 这个初始化器用于创建图层的影子副本, 例如,对于presentationLayer 方法。
子类可以选择将它们的实例变量复制到新的 对象。
子类应该总是调用超类实现
注意:在任何其他情况下调用此方法都会产生 未定义的行为。不要使用此方法初始化新层 与现有图层的内容。可用性 在 iOS 2.0 中可用 然后。在 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");
}
【问题讨论】:
标签: c# xamarin.ios core-animation overriding