这本质上与此处发布的问题相同:在 CALayer 中对自定义属性进行动画处理一年多前,尚未得到答复。
我创建一个自定义图层并在其上画一个圆圈。我希望能够对圆的半径(以及稍后的其他属性)进行动画处理。根据我读到的内容,我是这样设置的:
public class CircleLayer : CALayer
{
//[Export("radius")]
//public float Radius { get;set; }
//EDIT: I've now changed the radius field to what is coded below
public float Radius;
[Export("radius")]
public float getRadius()
{
return Radius;
}
[Export("setRadius:")]
public void setRadius(float val)
{
Radius = val;
}
public float Thickness {get;set;}
public CGColor Color {get;set;}
public float GlowAmount {get;set;}
private SizeF GlowOffset {get;set;}
[Export ("needsDisplayForKey:")]
static bool NeedsDisplayForKey (NSString key)
{
Console.WriteLine(key.ToString());
if(key.Equals("radius"))
{
return true;
}
else
return false;
}
public CircleLayer ()
{
if(GlowAmount == 0.0f)
GlowAmount = 10f;
GlowOffset = new SizeF(0f,0f);
//CALayer.NeedsDisplayForKey("radius");
}
public override void DrawInContext (CGContext context)
{
base.DrawInContext (context);
Console.WriteLine("drawing...........");
PointF centerPoint = new PointF(125,125);//this.Frame.Width/2,this.Frame.Height/2);
//Outer circle
context.AddEllipseInRect(new RectangleF(centerPoint.X - Radius,
centerPoint.Y - Radius,
Radius * 2,
Radius * 2));
//Inner circle
context.AddEllipseInRect(new RectangleF(centerPoint.X - InnerRadius,
centerPoint.Y - InnerRadius,
InnerRadius * 2,
InnerRadius * 2));
//Fill in circle
context.SetFillColor(Color);
context.SetShadowWithColor(GlowOffset,GlowAmount,GlowColor);
context.EOFillPath();
}
}
但它就是行不通。当 NeedsDisplayForKey 被调用(并将它们打印到控制台)时,我从未得到半径键的报告。我可以为标准属性设置动画没有问题(例如:比例)
编辑:请注意,我现在可以使用 SetValueForKey 成功修改属性 Radius 的值。如果我这样做,我需要调用 SetNeedsDisplay() 来更新屏幕,但是我仍然无法让动画正常工作。
编辑#2:附加示例:http://dl.dropbox.com/u/8617393/GraphicsTest1.zip