@@ -4,50 +4,77 @@ namespace MaterialDesignThemes.Wpf.Transitions;
4
4
5
5
public class FadeWipe : ITransitionWipe
6
6
{
7
- private readonly SineEase _sineEase = new SineEase ( ) ;
8
- private readonly KeyTime zeroKeyTime = KeyTime . FromTimeSpan ( TimeSpan . Zero ) ;
7
+ private readonly SineEase sineEase = new ( ) ;
8
+ private KeyTime startKeyTime , endKeyTime ;
9
+
10
+ private TimeSpan _duration = TimeSpan . FromMilliseconds ( 500 ) ;
11
+
12
+ public FadeWipe ( )
13
+ {
14
+ startKeyTime = KeyTime . FromTimeSpan ( TimeSpan . Zero ) ;
15
+ CalculateEndKeyTime ( ) ;
16
+ }
9
17
10
18
/// <summary>
11
19
/// Duration of the animation
12
20
/// </summary>
13
- public TimeSpan Duration { get ; set ; } = TimeSpan . FromSeconds ( 0.5 ) ;
21
+ public TimeSpan Duration
22
+ {
23
+ get => _duration ;
24
+ set
25
+ {
26
+ _duration = value ;
27
+ CalculateEndKeyTime ( ) ;
28
+ }
29
+ }
14
30
15
31
public void Wipe ( TransitionerSlide fromSlide , TransitionerSlide toSlide , Point origin , IZIndexController zIndexController )
16
32
{
17
- if ( fromSlide == null ) throw new ArgumentNullException ( nameof ( fromSlide ) ) ;
18
- if ( toSlide == null ) throw new ArgumentNullException ( nameof ( toSlide ) ) ;
19
- if ( zIndexController == null ) throw new ArgumentNullException ( nameof ( zIndexController ) ) ;
20
-
21
- // Set up time points
22
- var endKeyTime = KeyTime . FromTimeSpan ( TimeSpan . FromSeconds ( Duration . TotalSeconds / 2 ) ) ;
33
+ if ( fromSlide is null )
34
+ {
35
+ throw new ArgumentNullException ( nameof ( fromSlide ) ) ;
36
+ }
37
+ if ( toSlide is null )
38
+ {
39
+ throw new ArgumentNullException ( nameof ( toSlide ) ) ;
40
+ }
41
+ if ( zIndexController is null )
42
+ {
43
+ throw new ArgumentNullException ( nameof ( zIndexController ) ) ;
44
+ }
23
45
24
- // From
25
- var fromAnimation = new DoubleAnimationUsingKeyFrames ( ) ;
26
- fromAnimation . KeyFrames . Add ( new LinearDoubleKeyFrame ( 1 , zeroKeyTime ) ) ;
27
- fromAnimation . KeyFrames . Add ( new EasingDoubleKeyFrame ( 0 , endKeyTime , _sineEase ) ) ;
46
+ // Remove current animations and reset to base value
47
+ double currentFromOpacity = fromSlide . Opacity ;
48
+ fromSlide . BeginAnimation ( UIElement . OpacityProperty , null ) ;
49
+ fromSlide . Opacity = currentFromOpacity ;
28
50
29
- // To
30
- var toAnimation = new DoubleAnimationUsingKeyFrames ( ) ;
31
- toAnimation . KeyFrames . Add ( new LinearDoubleKeyFrame ( 0 , zeroKeyTime ) ) ;
32
- toAnimation . KeyFrames . Add ( new EasingDoubleKeyFrame ( 1 , endKeyTime , _sineEase ) ) ;
51
+ double currentToOpacity = toSlide . Opacity ;
52
+ toSlide . BeginAnimation ( UIElement . OpacityProperty , null ) ;
53
+ toSlide . Opacity = currentToOpacity != 1 ? currentToOpacity : 0 ;
33
54
34
- // Preset
35
- fromSlide . Opacity = 1 ;
36
- toSlide . Opacity = 0 ;
55
+ zIndexController . Stack ( toSlide , fromSlide ) ;
56
+ DoubleAnimationUsingKeyFrames fromAnimation = SetupOpacityAnimation ( currentFromOpacity , 0 ) ;
57
+ DoubleAnimationUsingKeyFrames toAnimation = SetupOpacityAnimation ( currentToOpacity , 1 ) ;
37
58
38
- // Set up events
39
- toAnimation . Completed += ( sender , args ) =>
40
- {
41
- toSlide . BeginAnimation ( UIElement . OpacityProperty , null ) ;
42
- } ;
43
59
fromAnimation . Completed += ( sender , args ) =>
44
60
{
45
61
fromSlide . BeginAnimation ( UIElement . OpacityProperty , null ) ;
46
62
toSlide . BeginAnimation ( UIElement . OpacityProperty , toAnimation ) ;
47
63
} ;
48
64
49
- // Animate
50
65
fromSlide . BeginAnimation ( UIElement . OpacityProperty , fromAnimation ) ;
51
- zIndexController . Stack ( toSlide , fromSlide ) ;
66
+ }
67
+
68
+ private DoubleAnimationUsingKeyFrames SetupOpacityAnimation ( double from , double to )
69
+ {
70
+ DoubleAnimationUsingKeyFrames animation = new ( ) ;
71
+ animation . KeyFrames . Add ( new LinearDoubleKeyFrame ( from , startKeyTime ) ) ;
72
+ animation . KeyFrames . Add ( new EasingDoubleKeyFrame ( to , endKeyTime , sineEase ) ) ;
73
+ return animation ;
74
+ }
75
+
76
+ private void CalculateEndKeyTime ( )
77
+ {
78
+ endKeyTime = KeyTime . FromTimeSpan ( TimeSpan . FromSeconds ( Duration . TotalSeconds / 2 ) ) ;
52
79
}
53
80
}
0 commit comments