Search code examples
c#xamarin.ioscore-animationoverriding

CALayer initWithLayer: bound to default constructor by mistake? Can I override the default constructor?


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:

[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);
        }
    }

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?

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 Documentation

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

EDIT:

The reason I need to override this method is the CoreAnimation framework calls this method to create its own internal copy of my layer. I need to be able to override it to include the new properties i've added to my layer, so that CoreAnimation knows to tween my property values over the course of the animation. I will not be calling this myself. This is why I cant simply call Clone()

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

Solution

  • 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:

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

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

    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?

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

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