0

カスタム プロパティの変更時に CALayer のアニメーションをトリガーしようとしています。円の半径を変更すると、レイヤーが自動的にアニメーションをトリガーするようにします。Objective-C では、属性をプロパティに設定し、メソッドをオーバーライドすることで (この例のように) これが可能になります。これにより、アニメーションが設定されます。@dynamicactionForKey:

public class MyCircle : CALayer
{
    [Export ("radius")]
    public float Radius { get; set; }

    public MyCircle ()
    {
        Radius = 200;
        SetNeedsDisplay ();
    }

    [Export ("initWithLayer:")]
    public MyCircle (CALayer other) : base (other) 
    { }

    public override void Clone (CALayer other)
    {
        base.Clone (other);
        MyCircle o = other as MyCircle;
        Radius = o.Radius;
    }

    public CABasicAnimation MakeAnimationForKey (String key)
    {
        CABasicAnimation animation = CABasicAnimation.FromKeyPath (key);
        animation.From = PresentationLayer.ValueForKey (new NSString (key));
        animation.Duration = 1;
        return animation;
    }

    [Export ("actionForKey:")]
    public override NSObject ActionForKey (string key)
    {
        switch (key.ToString ())
        {
            case "radius":
                return MakeAnimationForKey (key);
            default:
                return base.ActionForKey (key);
        }
    }

    [Export ("needsDisplayForKey:")]
    static bool NeedsDisplayForKey (NSString key)
    {
        switch (key.ToString ())
        {
            case "radius":
                return true;
            default:
                return CALayer.NeedsDisplayForKey (key);
        }
    }

    public override void DrawInContext (CGContext ctx)
    {
        // draw circle based in radius
    }
}

ただし、私の C#/Monotouch コードでは、値が変更されたときに「radius」が ActionForKey に送信されることはありません。前の質問 ( Animate a custom property using CoreAnimation in Monotouch? ) では、答えと提供されたサンプル コードは、手動で呼び出されるカスタム プロパティ アニメーションに基づいています (これは望ましくありません)。

(私が) 希望する動作は Monotouch でサポートされていますか? 私は何を間違っていますか?

4

1 に答える 1

0

コードにコンストラクターがありません:

[Export ("initWithLayer:")]
public MyCircle (CALayer other)
    : base (other)
{
}

問題が解決するかどうかはわかりませんが、試してみる価値はあります:)

于 2013-06-24T16:15:03.227 に答える