我正在尝试在更改自定义属性时触发CALayer的动画。当我改变圆的半径时,我希望图层自动触发它的动画。在Objective-C中,可以通过将@dynamic
属性设置为属性并覆盖actionForKey:
方法来实现(like in this example),这样就可以设置动画。
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是否支持(由我)所需的行为?我做错了什么?
答案 0 :(得分:0)
您在代码中缺少构造函数:
[Export ("initWithLayer:")]
public MyCircle (CALayer other)
: base (other)
{
}
我不确定它能解决你的问题,但值得一试:)