如何将 onFinish evant 添加到 Firemonkey 中的 TAnimate

How to add onFinish evant to the TAnimate in Firemonkey

我在 Delphi 10.3.3 中有代码:

 MainForm.Circle1.AnimateInt('Height', 150, 0.5, TAnimationType.InOut,
    TInterpolationType.Linear);

如何为此动画添加 OnFinish 事件,如 ShowMessage('Finish')

FMX.Controls.TControl methods Animate... are deprecated, and should not be used. You should use the Animate... methods of FMX.Ani.TAnimator代替,例如:

FMX.Ani.TAnimator.AnimateInt(Circle1, Height, 150, 0.5, TAnimationType.InOut, TInterpolationType.Linear);

但要添加 OnFinish() 事件,您需要采用另一种方法,方法是 (A) 在设计器中声明动画或 (B) 在代码中创建动画。

(A) 在设计器中声明动画

在此示例中,我们为表单中的 Circle1: TCircle 对象的 Height 属性 创建动画。

Select 对象。在Object Inspector(OI)中找到Height属性和select吧。请注意,它在值列中有一个幻灯片符号。这意味着您可以为其设置动画。请注意,值字段中有一个下拉箭头。单击它并从下拉菜单中单击 select Create new TFloatAnimation

在结构窗格中,您可以看到 Circle1 现在有一个名为 FloatAnimation1 的子对象。它应该是 selected,但如果不是,select 它。在 OI 中,您现在可以看到动画的所有已发布属性,如果您更改 OI 以显示事件,您将找到两个事件的设置,OnFinishOnProcess。双击会像往常一样为您创建事件。

(B) 在代码中创建动画

  1. 在表单的私有部分声明一个动画对象,并为 OnProcess and/or OnFinish 声明一个或两个事件处理程序。它们必须符合TNotifyEvent(即有一个参数,Sender: TObject
    private
      ...
      Anim: TFloatAnimation;
      procedure AnimProcess(Sender: TObject);
      procedure AnimFinish(Sender: TObject);
  1. 在表单的 OnCreate 事件中(例如 TForm1.FormCreate())创建 Anim 对象并根据需要设置其属性,例如
    Anim:= TFloatAnimation.Create(Self);
    Anim.OnProcess := AnimProcess;
    Anim.OnFinish := AnimFinish;
    Anim.Duration := 0.9;
    Anim.StartValue := 41;
    Anim.StopValue := 150;
    Anim.Parent := Circle1; // The object that the animation will affect must be the parent
    Anim.PropertyName := 'Height';

在这两种情况下都根据需要编写事件处理程序,例如

procedure TForm2.AnimFinish(Sender: TObject);
begin
  Memo1.Lines.Add('Animation finished');
end;