我试图在更改自定义属性时触发 CALayer 的动画。当我改变我的圆的半径时,我希望图层自动触发它的动画。在 Objective-C中,通过将属性设置为属性并覆盖方法,这是可能的(就像在这个例子中一样),这反过来又设置了动画。@dynamic
actionForKey:
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 代码中,当值更改时,“半径”永远不会发送到 ActionForKey。在上一个问题(Animate a custom property using CoreAnimation in Monotouch?)中,答案和提供的示例代码基于手动调用的自定义属性动画(我不希望这样做)。
Monotouch 是否支持(我)期望的行为?我究竟做错了什么?