我的表单上有一个TScrollbox。放置在Scrollbox上的是一个TLayout,它比设备视口更宽和更高,因此可以显示水平和垂直滚动条,并且用户可以手动移动布局。

我还有一个手势设置,以便用户可以长按Scrollbox并使布局返回原点(0,0)。

procedure TfrmMain.ScrollBox1Gesture(Sender: TObject;
  const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
  if EventInfo.GestureID = System.UITypes.igiLongTap then
  begin
     ScrollBox1.ViewportPosition := PointF(0, 0);
     ScrollBox1.RealignContent;
  end;


现在这很好用,但是很快就可以了。我想也许我可以使用:

TAnimator.AnimateFloat(Scrollbox1, 'ScrollBox1.ViewportPosition.X', 0, 0.3);


使从当前位置回到0的移动更加平缓,但这当然是行不通的,因为您不能直接为PointF.XPointF.Y赋值(因此动画师也不能) 。

那怎么办呢?
谢谢

最佳答案

由于缺少内置的PointAnimation,因此这里是一个自写的。

type
  TPointAnimation = class(TCustomPropertyAnimation)
  private
    FStartFloat: TPointF;
    FStartFromCurrent: Boolean;
    FStopFloat: TPointF;
  protected
    procedure FirstFrame; override;
    procedure ProcessAnimation; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property AnimationType default TAnimationType.In;
    property AutoReverse default False;
    property Delay;
    property Duration nodefault;
    property Enabled default False;
    property Interpolation default TInterpolationType.Linear;
    property Inverse default False;
    property Loop default False;
    property OnFinish;
    property OnProcess;
    property PropertyName;
    property StartFromCurrent: Boolean read FStartFromCurrent write
        FStartFromCurrent default False;
    property StartValue: TPointF read FStartFloat write FStartFloat stored True;
    property StopValue: TPointF read FStopFloat write FStopFloat stored True;
    property Trigger;
    property TriggerInverse;
  end;

constructor TPointAnimation.Create(AOwner: TComponent);
begin
  inherited;
  Duration := 0.2;
  FStartFloat := PointF(0, 0);
  FStopFloat := PointF(0, 0);
end;

procedure TPointAnimation.FirstFrame;
var
  T: TRttiType;
  P: TRttiProperty;
begin
  if StartFromCurrent then
  begin
    T := SharedContext.GetType(FInstance.ClassInfo);
    if T <> nil then
    begin
      P := T.GetProperty(FPath);
      if (P <> nil) and (P.PropertyType.TypeKind = tkRecord) then
        StartValue := P.GetValue(FInstance).AsType<TPointF>;
    end;
  end;
end;

procedure TPointAnimation.ProcessAnimation;
var
  newPoint: TPointF;
  T: TRttiType;
  P: TRttiProperty;
begin
  if FInstance <> nil then
  begin
    T := SharedContext.GetType(FInstance.ClassInfo);
    if T <> nil then
    begin
      P := T.GetProperty(FPath);
      if (P <> nil) and (P.PropertyType.TypeKind = tkRecord) then begin
        newPoint := PointF(InterpolateSingle(FStartFloat.X, FStopFloat.X, NormalizedTime),
                           InterpolateSingle(FStartFloat.Y, FStopFloat.Y, NormalizedTime));
        P.SetValue(FInstance, TValue.From<TPointF>(newPoint));
      end;
    end;
  end;
end;


在表单私有部分中声明一个字段

ani: TPointAnimation;


在FormCreate期间创建实例

ani := TPointAnimation.Create(Self);
ani.PropertyName := 'ViewportPosition';
ani.StartFromCurrent := true;
ani.Duration := 0.3;
ScrollBox1.AddObject(ani);


并在需要时启动

ani.Start;

10-04 18:37