CALayer initWithLayer:错误地绑定到默认构造函数?我可以覆盖默认构造函数吗? [英] CALayer initWithLayer: bound to default constructor by mistake? Can I override the default constructor?

查看:141
本文介绍了CALayer initWithLayer:错误地绑定到默认构造函数?我可以覆盖默认构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

[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类中的自定义属性设置动画. (如我上一个问题中所述使用Monotouch中的CoreAnimation对自定义属性进行动画处理? )

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? )

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

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

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

Apples Documentation states that the only use of initWithLayer is to create shadow copies, its not for initializing a layer, which makes me thing it might be a Monotouch bug

Apple文档

initWithLayer:重写以复制或初始化 指定的图层.

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

  • (id)initWithLayer:(id)layer参数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.

讨论此初始化程序用于创建图层的卷影副本, 例如,对于presentationLayer方法.

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

注意:在任何其他情况下调用此方法都将产生 未定义的行为.不要使用此方法初始化新层 包含现有图层的内容.可用性在iOS 2.0中可用 然后.在CALayer.h中声明

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的代码进行修改,但仍然无法调用:

I've tried to add a modification to Rolf's code, but it still doesn't get called:

//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");
    }

推荐答案

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

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

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

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

一点用语:public CALayer (CALayer other)不是默认的构造函数-默认的构造函数没有任何参数.

A little bit of terminology: public CALayer (CALayer other) isn't a default constructor - a default constructor doesn't have any parameters.

但是,我需要能够覆盖initWithLayer以便为CALayer类中的自定义属性设置动画. (正如我在上一个问题中指定的那样,在Monotouch中使用CoreAnimation对自定义属性进行动画处理?)

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构造函数.我确实找到了这个:

That question doesn't say why you need to override the initWithLayer constructor. I did find this though: Why animating custom CALayer properties causes other properties to be nil during animation?, which seems to be what you're trying to do. In this case I think there is an easier way to accomplish your need: you just need to override the Clone method in your custom CALayer class, and do the proper initialization there.

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

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

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

Technically, in C# you don't override a constructor. In your base class you provide your own constructors, and all of them must call one constructor in the immediate base class (this may be done implicitly by the compiler in some cases).

也就是说,在MonoTouch中,您可以在您的类中提供一个构造函数,该构造函数可以完成您想要的所有事情(不过我仍然相信您应该首先尝试使用Clone方法):

That said, in MonoTouch you can provide a constructor in your class that does everything you want (I still believe you should try the Clone approach first though):

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.
    }
}

这篇关于CALayer initWithLayer:错误地绑定到默认构造函数?我可以覆盖默认构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆