방법: 키 프레임 애니메이션 타이밍 제어
이 예제에서는 키 프레임 애니메이션 내에서 키 프레임의 타이밍을 제어하는 방법을 보여 줍니다. 다른 애니메이션과 마찬가지로 키 프레임 애니메이션에는 Duration 속성이 있습니다. 애니메이션의 지속 시간을 지정하는 것 외에도 지속 시간의 어느 부분이 각 키 프레임에 할당되는지를 지정해야 합니다. 시간을 할당하려면 애니메이션의 각 키 프레임에 KeyTime을 지정합니다.
각 키 프레임에 대한 KeyTime은 키 프레임이 종료되는 시기를 지정합니다(키 프레임이 재생되는 시간의 길이를 지정하지 않음). KeyTime을 백분율인 TimeSpan 또는 Uniform나 Paced 특수 값으로 지정할 수 있습니다.
예제
다음 예제에서는 화면에 사각형 애니메이션 효과를 주기 위해 DoubleAnimationUsingKeyFrames를 사용합니다. 키 프레임의 키 시간은 TimeSpan 값으로 설정됩니다.
/*
This Rectangle is animated with KeyTimes using TimeSpan Values.
It moves horizontally to 100 in the first 3 seconds, 100 to 300 in
the next second, and 300 to 500 in the last 6 seconds.
*/
// Create the a rectangle.
Rectangle aRectangle = new Rectangle();
aRectangle.Fill = Brushes.Blue;
aRectangle.Stroke = Brushes.Black;
aRectangle.StrokeThickness = 5;
aRectangle.Width = 50;
aRectangle.Height = 50;
// Create a transform to move the rectangle
// across the screen.
TranslateTransform translateTransform1 =
new TranslateTransform();
aRectangle.RenderTransform = translateTransform1;
// Create a DoubleAnimationUsingKeyFrames
// to animate the transform.
DoubleAnimationUsingKeyFrames transformAnimation =
new DoubleAnimationUsingKeyFrames();
transformAnimation.Duration = TimeSpan.FromSeconds(10);
// Animate to 100 at 3 seconds.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(100, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(3))));
// Animate to 300 at 4 seconds.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(300, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(4))));
// Animate to 500 at 10 seconds.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(500, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(10))));
// Start the animation when the rectangle is loaded.
aRectangle.Loaded += delegate(object sender, RoutedEventArgs e)
{
translateTransform1.BeginAnimation(TranslateTransform.XProperty, transformAnimation);
};
'
' This Rectangle is animated with KeyTimes using TimeSpan Values.
' It moves horizontally to 100 in the first 3 seconds, 100 to 300 in
' the next second, and 300 to 500 in the last 6 seconds.
'
' Create the a rectangle.
Dim aRectangle As New Rectangle()
aRectangle.Fill = Brushes.Blue
aRectangle.Stroke = Brushes.Black
aRectangle.StrokeThickness = 5
aRectangle.Width = 50
aRectangle.Height = 50
' Create a transform to move the rectangle
' across the screen.
Dim translateTransform1 As New TranslateTransform()
aRectangle.RenderTransform = translateTransform1
' Create a DoubleAnimationUsingKeyFrames
' to animate the transform.
Dim transformAnimation As New DoubleAnimationUsingKeyFrames()
transformAnimation.Duration = TimeSpan.FromSeconds(10)
' Animate to 100 at 3 seconds.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(100, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(3))))
' Animate to 300 at 4 seconds.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(300, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(4))))
' Animate to 500 at 10 seconds.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(500, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(10))))
' Start the animation when the rectangle is loaded.
AddHandler aRectangle.Loaded, Sub(sender As Object, e As RoutedEventArgs) translateTransform1.BeginAnimation(TranslateTransform.XProperty, transformAnimation)
<!-- This Rectangle is animated with KeyTimes using TimeSpan Values.
It moves horizontally to 100 in the first 3 seconds, 100 to 300 in
the next second, and 300 to 500 in the last 6 seconds. -->
<Rectangle Fill="Blue" Stroke="Black" StrokeThickness="5"
Width="50" Height="50">
<Rectangle.RenderTransform>
<TranslateTransform x:Name="TranslateTransform1" />
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="TranslateTransform1"
Storyboard.TargetProperty="X"
Duration="0:0:10">
<!-- These KeyTime properties are specified as TimeSpan values
which are in the form of "hours:minutes:seconds". -->
<LinearDoubleKeyFrame Value="100" KeyTime="0:0:3" />
<LinearDoubleKeyFrame Value="300" KeyTime="0:0:4" />
<LinearDoubleKeyFrame Value="500" KeyTime="0:0:10" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
다음 그림은 각 키 프레임의 값에 도달하는 시점을 보여 줍니다.
값이 0, 3, 4 및 10초인 파란색 그라데이션 색이 증가하는 4개의 키 프레임을 보여주는 스크린샷.graphicsmm_keyframe_keytime1_timespan
다음 예제에서는 키 프레임의 키 시간이 백분율 값으로 설정된다는 점을 제외하고 동일한 애니메이션을 보여 줍니다.
/*
This rectangle moves horizontally to 100 in the first 3 seconds,
100 to 300 in the next second, and 300 to 500 in the last 6 seconds.
*/
// Create the a rectangle.
Rectangle aRectangle = new Rectangle();
aRectangle.Fill = Brushes.Purple;
aRectangle.Stroke = Brushes.Black;
aRectangle.StrokeThickness = 5;
aRectangle.Width = 50;
aRectangle.Height = 50;
// Create a transform to move the rectangle
// across the screen.
TranslateTransform translateTransform2 =
new TranslateTransform();
aRectangle.RenderTransform = translateTransform2;
// Create a DoubleAnimationUsingKeyFrames
// to animate the transform.
DoubleAnimationUsingKeyFrames transformAnimation =
new DoubleAnimationUsingKeyFrames();
transformAnimation.Duration = TimeSpan.FromSeconds(10);
// Animate to 100 at 30% of the animation's duration.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(100, KeyTime.FromPercent(0.3)));
// Animate to 300 at 40% of the animation's duration.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(300, KeyTime.FromPercent(0.4)));
// Animate to 500 at 100% of the animation's duration.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(500, KeyTime.FromPercent(1.0)));
// Start the animation when the rectangle is loaded.
aRectangle.Loaded += delegate(object sender, RoutedEventArgs e)
{
translateTransform2.BeginAnimation(TranslateTransform.XProperty, transformAnimation);
};
'
' This rectangle moves horizontally to 100 in the first 3 seconds,
' 100 to 300 in the next second, and 300 to 500 in the last 6 seconds.
'
' Create the a rectangle.
Dim aRectangle As New Rectangle()
aRectangle.Fill = Brushes.Purple
aRectangle.Stroke = Brushes.Black
aRectangle.StrokeThickness = 5
aRectangle.Width = 50
aRectangle.Height = 50
' Create a transform to move the rectangle
' across the screen.
Dim translateTransform2 As New TranslateTransform()
aRectangle.RenderTransform = translateTransform2
' Create a DoubleAnimationUsingKeyFrames
' to animate the transform.
Dim transformAnimation As New DoubleAnimationUsingKeyFrames()
transformAnimation.Duration = TimeSpan.FromSeconds(10)
' Animate to 100 at 30% of the animation's duration.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(100, KeyTime.FromPercent(0.3)))
' Animate to 300 at 40% of the animation's duration.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(300, KeyTime.FromPercent(0.4)))
' Animate to 500 at 100% of the animation's duration.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(500, KeyTime.FromPercent(1.0)))
' Start the animation when the rectangle is loaded.
AddHandler aRectangle.Loaded, Sub(sender As Object, e As RoutedEventArgs) translateTransform2.BeginAnimation(TranslateTransform.XProperty, transformAnimation)
<!-- This rectangle moves horizontally to 100 in the first 3 seconds,
100 to 300 in the next second, and 300 to 500 in the last 6 seconds.-->
<Rectangle Fill="Purple" Stroke="Black" StrokeThickness="5"
Width="50" Height="50">
<Rectangle.RenderTransform>
<TranslateTransform x:Name="TranslateTransform2" />
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="TranslateTransform2"
Storyboard.TargetProperty="X"
Duration="0:0:10">
<!-- KeyTime properties are expressed as Percentages. -->
<LinearDoubleKeyFrame Value="100" KeyTime="30%" />
<LinearDoubleKeyFrame Value="300" KeyTime="40%" />
<LinearDoubleKeyFrame Value="500" KeyTime="100%" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
다음 그림은 각 키 프레임의 값에 도달하는 시점을 보여 줍니다.
값이 0, 3, 4 및 10초인 보라색 그라데이션 색이 증가하는 4개의 키 프레임을 보여주는 스크린샷.graphicsmm_keyframe_keytime2_percentage
다음 예제에서는 Uniform 키 시간 값을 사용합니다.
/*
This rectangle is animated with KeyTimes using Uniform values.
Goes to 100 in the first 3.3 seconds, 100 to
300 in the next 3.3 seconds, 300 to 500 in the last 3.3 seconds.
*/
// Create the a rectangle.
Rectangle aRectangle = new Rectangle();
aRectangle.Fill = Brushes.Red;
aRectangle.Stroke = Brushes.Black;
aRectangle.StrokeThickness = 5;
aRectangle.Width = 50;
aRectangle.Height = 50;
// Create a transform to move the rectangle
// across the screen.
TranslateTransform translateTransform3 =
new TranslateTransform();
aRectangle.RenderTransform = translateTransform3;
// Create a DoubleAnimationUsingKeyFrames
// to animate the transform.
DoubleAnimationUsingKeyFrames transformAnimation =
new DoubleAnimationUsingKeyFrames();
transformAnimation.Duration = TimeSpan.FromSeconds(10);
/*
KeyTime properties are expressed with values of Uniform. When a key time is set to
"Uniform" the total allotted time of the animation is divided evenly between key frames.
In this example, the total duration of the animation is ten seconds and there are four
key frames each of which are set to "Uniform", therefore, the duration of each key frame
is 3.3 seconds (10/3).
*/
// Animate to 100.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(100, KeyTime.Uniform));
// Animate to 300.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(300, KeyTime.Uniform));
// Animate to 500.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(500, KeyTime.Uniform));
// Start the animation when the rectangle is loaded.
aRectangle.Loaded += delegate(object sender, RoutedEventArgs e)
{
translateTransform3.BeginAnimation(TranslateTransform.XProperty, transformAnimation);
};
'
' This rectangle is animated with KeyTimes using Uniform values.
' Goes to 100 in the first 3.3 seconds, 100 to
' 300 in the next 3.3 seconds, 300 to 500 in the last 3.3 seconds.
'
' Create the a rectangle.
Dim aRectangle As New Rectangle()
aRectangle.Fill = Brushes.Red
aRectangle.Stroke = Brushes.Black
aRectangle.StrokeThickness = 5
aRectangle.Width = 50
aRectangle.Height = 50
' Create a transform to move the rectangle
' across the screen.
Dim translateTransform3 As New TranslateTransform()
aRectangle.RenderTransform = translateTransform3
' Create a DoubleAnimationUsingKeyFrames
' to animate the transform.
Dim transformAnimation As New DoubleAnimationUsingKeyFrames()
transformAnimation.Duration = TimeSpan.FromSeconds(10)
'
' KeyTime properties are expressed with values of Uniform. When a key time is set to
' "Uniform" the total allotted time of the animation is divided evenly between key frames.
' In this example, the total duration of the animation is ten seconds and there are four
' key frames each of which are set to "Uniform", therefore, the duration of each key frame
' is 3.3 seconds (10/3).
'
' Animate to 100.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(100, KeyTime.Uniform))
' Animate to 300.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(300, KeyTime.Uniform))
' Animate to 500.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(500, KeyTime.Uniform))
' Start the animation when the rectangle is loaded.
AddHandler aRectangle.Loaded, Sub(sender As Object, e As RoutedEventArgs) translateTransform3.BeginAnimation(TranslateTransform.XProperty, transformAnimation)
<!-- This rectangle is animated with KeyTimes using Uniform values.
Goes to 100 in the first 3.3 seconds, 100 to
300 in the next 3.3 seconds, 300 to 500 in the last 3.3 seconds. -->
<Rectangle Fill="Red" Stroke="Black" StrokeThickness="5"
Width="50" Height="50">
<Rectangle.RenderTransform>
<TranslateTransform x:Name="TranslateTransform3" />
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="TranslateTransform3"
Storyboard.TargetProperty="X"
Duration="0:0:10">
<!-- KeyTime properties are expressed with values of Uniform. When a key time is set to
"Uniform" the total allotted time of the animation is divided evenly between key frames.
In this example, the total duration of the animation is ten seconds and there are four
key frames each of which are set to "Uniform", therefore, the duration of each key frame
is 3.3 seconds (10/3). -->
<LinearDoubleKeyFrame Value="100" KeyTime="Uniform" />
<LinearDoubleKeyFrame Value="300" KeyTime="Uniform" />
<LinearDoubleKeyFrame Value="500" KeyTime="Uniform" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
다음 그림은 각 키 프레임의 값에 도달하는 시점을 보여 줍니다.
키 값은 3.3,6.6, 9.9초에 도달graphicsmm_keyframe_keytime3_uniform
최종 예제에서는 Paced 키 시간 값을 사용합니다.
/*
This rectangle is animated with KeyTimes using Paced Values.
The rectangle moves between key frames at uniform rate except for first key frame
because using a Paced value on the first KeyFrame in a collection of frames gives a time of zero.
*/
// Create the a rectangle.
Rectangle aRectangle = new Rectangle();
aRectangle.Fill = Brushes.Orange;
aRectangle.Stroke = Brushes.Black;
aRectangle.StrokeThickness = 5;
aRectangle.Width = 50;
aRectangle.Height = 50;
// Create a transform to move the rectangle
// across the screen.
TranslateTransform translateTransform4 =
new TranslateTransform();
aRectangle.RenderTransform = translateTransform4;
// Create a DoubleAnimationUsingKeyFrames
// to animate the transform.
DoubleAnimationUsingKeyFrames transformAnimation =
new DoubleAnimationUsingKeyFrames();
transformAnimation.Duration = TimeSpan.FromSeconds(10);
/*
Use Paced values when a constant rate is desired.
The time allocated to a key frame with a KeyTime of "Paced" is
determined by the time allocated to the other key frames of the animation. This time is
calculated to attempt to give a "paced" or "constant velocity" for the animation.
*/
// Animate to 100.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(100, KeyTime.Paced));
// Animate to 300.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(300, KeyTime.Paced));
// Animate to 500.
transformAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(500, KeyTime.Paced));
// Start the animation when the rectangle is loaded.
aRectangle.Loaded += delegate(object sender, RoutedEventArgs e)
{
translateTransform4.BeginAnimation(TranslateTransform.XProperty, transformAnimation);
};
'
' This rectangle is animated with KeyTimes using Paced Values.
' The rectangle moves between key frames at uniform rate except for first key frame
' because using a Paced value on the first KeyFrame in a collection of frames gives a time of zero.
'
' Create the a rectangle.
Dim aRectangle As New Rectangle()
aRectangle.Fill = Brushes.Orange
aRectangle.Stroke = Brushes.Black
aRectangle.StrokeThickness = 5
aRectangle.Width = 50
aRectangle.Height = 50
' Create a transform to move the rectangle
' across the screen.
Dim translateTransform4 As New TranslateTransform()
aRectangle.RenderTransform = translateTransform4
' Create a DoubleAnimationUsingKeyFrames
' to animate the transform.
Dim transformAnimation As New DoubleAnimationUsingKeyFrames()
transformAnimation.Duration = TimeSpan.FromSeconds(10)
'
' Use Paced values when a constant rate is desired.
' The time allocated to a key frame with a KeyTime of "Paced" is
' determined by the time allocated to the other key frames of the animation. This time is
' calculated to attempt to give a "paced" or "constant velocity" for the animation.
'
' Animate to 100.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(100, KeyTime.Paced))
' Animate to 300.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(300, KeyTime.Paced))
' Animate to 500.
transformAnimation.KeyFrames.Add(New LinearDoubleKeyFrame(500, KeyTime.Paced))
' Start the animation when the rectangle is loaded.
AddHandler aRectangle.Loaded, Sub(sender As Object, e As RoutedEventArgs) translateTransform4.BeginAnimation(TranslateTransform.XProperty, transformAnimation)
<!-- This rectangle is animated with KeyTimes using Paced Values.
The rectangle moves between key frames at uniform rate except for first key frame
because using a Paced value on the first KeyFrame in a collection of frames gives a time of zero. -->
<Rectangle Fill="Orange" Stroke="Black" StrokeThickness="5"
Width="50" Height="50">
<Rectangle.RenderTransform>
<TranslateTransform x:Name="TranslateTransform4" />
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="TranslateTransform4"
Storyboard.TargetProperty="X"
Duration="0:0:10">
<!-- Use Paced values when a constant rate is desired.
The time allocated to a key frame with a KeyTime of "Paced" is
determined by the time allocated to the other key frames of the animation. This time is
calculated to attempt to give a "paced" or "constant velocity" for the animation. -->
<LinearDoubleKeyFrame Value="100" KeyTime="Paced" />
<LinearDoubleKeyFrame Value="300" KeyTime="Paced" />
<LinearDoubleKeyFrame Value="500" KeyTime="Paced" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
다음 그림은 각 키 프레임의 값에 도달하는 시점을 보여 줍니다.
키 값은 0, 5 및 10초에 도달graphicsmm_keyframe_keytime4_paced
간단히 하기 위해 이 예제의 코드 버전에서는 단일 애니메이션만 단일 속성에 적용되므로 스토리보드가 아닌 로컬 애니메이션을 사용하지만 대신 스토리보드를 사용하도록 예제를 수정할 수 있습니다. 코드에서 스토리보드를 선언하는 방법을 보여 주는 예제는 Storyboard를 사용하여 속성에 애니메이션 효과 주기를 참조하세요.
전체 샘플을 보려면 키 프레임 애니메이션 샘플을 참조하세요. 키 프레임 애니메이션에 대한 자세한 내용은 키 프레임 애니메이션 개요를 참조하세요.
참고 항목
.NET Desktop feedback