Firemonkey里实现页面切换动画效果

5bug 2018-01-04 72人围观 ,发现0个评论 Firemonkey跨平台Firemonkey动画TFrameAnimation

大家在使用APP的时候,几乎每个APP都有页面切换的动画效果,那么我们在FireMonkey里做跨平台开发的时候,如何实现这种动画效果呢?我的“由由密码管家”里实现了这个效果,这里给出具体的实现方法,直接贴上代码吧:

unit uFMX.FrameAnimation;

interface

uses
  System.Classes, FMX.Types, FMX.Ani;

type
  TFrameAnimation = class(TFloatAnimation)
  private
    FOnAnimationFinish: TNotifyEvent;
  protected
    procedure DoFinish; override;
  public
    class procedure AnimationShow(const Target: TFmxObject; const APropertyName: string; const NewValue: Single;
      Duration: Single = 0.2; AType: TAnimationType = TAnimationType.In;
      AInterpolation: TInterpolationType = TInterpolationType.Linear; OnAnimationFinish: TNotifyEvent = nil); overload;
    class procedure AnimationShow(const Target: TFmxObject; const APropertyName: string; const NewValue: Single;
      OnAnimationFinish: TNotifyEvent = nil); overload;
  end;

implementation

{ TFrameAnimation }

class procedure TFrameAnimation.AnimationShow(const Target: TFmxObject; const APropertyName: string;
  const NewValue: Single; Duration: Single; AType: TAnimationType; AInterpolation: TInterpolationType;
  OnAnimationFinish: TNotifyEvent);
var
  Animation: TFrameAnimation;
begin
  TAnimator.StopPropertyAnimation(Target, APropertyName);

  Animation := TFrameAnimation.Create(nil);
  Animation.Parent := Target;
  Animation.AnimationType := AType;
  Animation.Interpolation := AInterpolation;
  Animation.FOnAnimationFinish := OnAnimationFinish;
  Animation.Duration := Duration;
  Animation.PropertyName := APropertyName;
  Animation.StartFromCurrent := True;
  Animation.StopValue := NewValue;
  Animation.Start;
end;

class procedure TFrameAnimation.AnimationShow(const Target: TFmxObject; const APropertyName: string;
  const NewValue: Single; OnAnimationFinish: TNotifyEvent);
begin
  AnimationShow(Target, APropertyName, NewValue, 0.3, TAnimationType.In, TInterpolationType.Linear, OnAnimationFinish);
end;

procedure TFrameAnimation.DoFinish;
begin
  if Assigned(FOnAnimationFinish) then
    FOnAnimationFinish(Self);
  Parent := nil;
  inherited;
  DisposeOf;
end;

end.

具体使用方法如下,我这里是在TFrame的基础上继承了一个TBaseFrame,然后重写Show方法:

procedure TBaseFrame.Show;
begin
  inherited;
  if IsAnimationShow then
  begin
    Position.X := 0;
    Position.Y := Height;
    if Parent is TCommonCustomForm then
    begin
      Width := TCommonCustomForm(Parent).Width;
      Height := TCommonCustomForm(Parent).Height;
    end;
    //从下往上的动画   还可以实现其他的,指定第二个参数相关的属性字符即可
    TFrameAnimation.AnimationShow(Self, 'Position.Y', 0, DoAnimationFinish);
  end
  else
  begin
    Self.Align := TAlignLayout.Contents;
    DoShow;
  end;
end;


请扫码加入QQ群
微信二维码
不容错过
Powered By Z-BlogPHP