forked from HemulGM/Components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHGM.FMX.Ani.pas
109 lines (91 loc) · 3.48 KB
/
HGM.FMX.Ani.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
unit HGM.FMX.Ani;
interface
uses
FMX.Ani, FMX.Types, System.Classes, System.SysUtils;
type
TAniFreeNotification = class(TInterfacedObject, IFreeNotification)
private
FProc: TProc;
public
procedure FreeNotification(AObject: TObject);
constructor Create(Proc: TProc);
end;
TAnimatorHelper = class helper for TAnimator
class procedure DetachPropertyAnimation(const Target: TFmxObject; const APropertyName: string);
class procedure AnimateFloat(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Update: TNotifyEvent; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear); overload;
class procedure AnimateFloatWithFinish(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Finish: TProc; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear); overload;
end;
implementation
{ TAnimatorHelper }
class procedure TAnimatorHelper.AnimateFloat(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Update: TNotifyEvent; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear);
var
Animation: TFloatAnimation;
begin
StopPropertyAnimation(Target, APropertyName);
with Self do
CreateDestroyer;
Animation := TFloatAnimation.Create(nil);
Animation.Parent := Target;
Animation.AnimationType := AType;
Animation.Interpolation := AInterpolation;
Animation.Duration := Duration;
Animation.PropertyName := APropertyName;
Animation.StartFromCurrent := True;
Animation.StopValue := NewValue;
Animation.OnProcess := Update;
with Self do
FDestroyer.RegisterAnimation(Animation);
Animation.Start;
end;
class procedure TAnimatorHelper.AnimateFloatWithFinish(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Finish: TProc; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear);
var
Animation: TFloatAnimation;
begin
StopPropertyAnimation(Target, APropertyName);
with Self do
CreateDestroyer;
Animation := TFloatAnimation.Create(nil);
Animation.Parent := Target;
Animation.AnimationType := AType;
Animation.Interpolation := AInterpolation;
Animation.Duration := Duration;
Animation.PropertyName := APropertyName;
Animation.StartFromCurrent := True;
Animation.StopValue := NewValue;
Animation.AddFreeNotify(TAniFreeNotification.Create(Finish));
with Self do
FDestroyer.RegisterAnimation(Animation);
Animation.Start;
end;
class procedure TAnimatorHelper.DetachPropertyAnimation(const Target: TFmxObject; const APropertyName: string);
var
I: Integer;
begin
I := Target.ChildrenCount - 1;
while I >= 0 do
begin
if (Target.Children[I] is TCustomPropertyAnimation) and
(CompareText(TCustomPropertyAnimation(Target.Children[I]).PropertyName, APropertyName) = 0) then
begin
var Anim := TFloatAnimation(Target.Children[I]);
Anim.Parent := nil;
Anim.Stop;
end;
if I > Target.ChildrenCount then
I := Target.ChildrenCount;
Dec(I);
end;
end;
{ TAniFreeNotification }
constructor TAniFreeNotification.Create(Proc: TProc);
begin
inherited Create;
FProc := Proc;
end;
procedure TAniFreeNotification.FreeNotification(AObject: TObject);
begin
if Assigned(FProc) then
FProc;
Free;
end;
end.