방법: Storyboard를 사용하여 속성에 애니메이션 효과 주기
업데이트: 2007년 11월
이 예제에서는 Storyboard를 사용하여 속성에 애니메이션 효과를 주는 방법을 보여 줍니다. Storyboard를 사용하여 속성에 애니메이션 효과를 주려면 애니메이션 효과를 줄 각 속성에 대해 애니메이션을 만들고 애니메이션이 포함될 Storyboard도 만듭니다.
사용할 애니메이션의 형식은 속성의 형식에 따라 결정됩니다. 예를 들어 Double 값을 사용하는 속성에 애니메이션 효과를 주려면 DoubleAnimation을 사용합니다. TargetName 및 TargetProperty연결된 속성을 사용하여 애니메이션이 적용되는 개체와 속성을 지정합니다.
XAML(Extensible Application Markup Language)에서 스토리보드를 시작하려면 BeginStoryboard 동작과 EventTrigger를 사용합니다. EventTrigger는 RoutedEvent 속성으로 지정된 이벤트가 발생하면 BeginStoryboard 동작을 시작합니다. BeginStoryboard 동작은 Storyboard를 시작합니다.
다음 예제에서는 Storyboard 개체를 사용하여 두 개의 Button 컨트롤에 애니메이션 효과를 줍니다. 첫 번째 단추의 크기를 변경하기 위해 Width에 애니메이션이 적용됩니다. 두 번째 단추의 색을 변경하기 위해 SolidColorBrush의 Color 속성을 사용하여 애니메이션 효과가 적용되는 단추의 Background를 설정합니다.
예제
<!-- StoryboardExample.xaml
Uses storyboards to animate properties. -->
<Page
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="Animate Properties with Storyboards">
<Border Background="White">
<StackPanel Margin="30" HorizontalAlignment="Left" MinWidth="500">
<TextBlock>Storyboard Animation Example</TextBlock>
<!-- The width of this button is animated. -->
<Button Name="myWidthAnimatedButton"
Height="30" Width="200" HorizontalAlignment="Left">
A Button
<Button.Triggers>
<!-- Animates the width of the first button
from 200 to 300. -->
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="myWidthAnimatedButton"
Storyboard.TargetProperty="Width"
From="200" To="300" Duration="0:0:3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<!-- The color of the brush used to paint this button is animated. -->
<Button Height="30" Width="200"
HorizontalAlignment="Left">Another Button
<Button.Background>
<SolidColorBrush x:Name="myAnimatedBrush" Color="Blue" />
</Button.Background>
<Button.Triggers>
<!-- Animates the color of the brush used to paint
the second button from red to blue . -->
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="myAnimatedBrush"
Storyboard.TargetProperty="Color"
From="Red" To="Blue" Duration="0:0:7" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
</Border>
</Page>
참고
Control 또는 Panel과 같은 FrameworkElement 개체와 Brush 또는 Transform과 같은 Freezable 개체가 모두 애니메이션의 대상이 될 수 있지만 프레임워크 요소만 Name 속성을 가집니다. Freezable에 이름을 할당하여 애니메이션 대상이 될 수 있도록 하려면 이전 예제에서처럼 x:Name 특성을 사용합니다.
코드를 사용하는 경우에는 FrameworkElement에 대한 NameScope를 만들고 애니메이션 효과를 줄 개체의 이름을 이 FrameworkElement에 등록합니다. 코드에서 애니메이션을 시작하려면 BeginStoryboard 동작을 EventTrigger와 함께 사용합니다. 필요하다면 이벤트 처리기와 Storyboard의 Begin 메서드를 사용할 수 있습니다. 다음 예제에서는 Begin 메서드를 사용하는 방법을 보여 줍니다.
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Namespace SDKSample
' Uses a storyboard to animate the properties
' of two buttons.
Public Class StoryboardExample
Inherits Page
Private Dim WithEvents myWidthAnimatedButton As Button
Private Dim WithEvents myColorAnimatedButton As Button
Private Dim myWidthAnimatedButtonStoryboard As Storyboard
Private Dim myColorAnimatedButtonStoryboard As Storyboard
Public Sub New()
' Create a name scope for the page.
NameScope.SetNameScope(Me, New NameScope())
Me.WindowTitle = "Animate Properties using Storyboards"
Dim myStackPanel As New StackPanel()
myStackPanel.MinWidth = 500
myStackPanel.Margin = New Thickness(30)
myStackPanel.HorizontalAlignment = HorizontalAlignment.Left
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Storyboard Animation Example"
myStackPanel.Children.Add(myTextBlock)
'
' Create and animate the first button.
'
' Create a button.
myWidthAnimatedButton = New Button()
myWidthAnimatedButton.Height = 30
myWidthAnimatedButton.Width = 200
myWidthAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left
myWidthAnimatedButton.Content = "A Button"
' Set the Name of the button so that it can be referred
' to in the storyboard that's created later.
' The ID doesn't have to match the variable name;
' it can be any unique identifier.
myWidthAnimatedButton.Name = "myWidthAnimatedButton"
' Register the name with the page to which the button belongs.
Me.RegisterName(myWidthAnimatedButton.Name, myWidthAnimatedButton)
' Create a DoubleAnimation to animate the width of the button.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.From = 200
myDoubleAnimation.To = 300
myDoubleAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(3000))
' Configure the animation to target the button's Width property.
Storyboard.SetTargetName(myDoubleAnimation, myWidthAnimatedButton.Name)
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Button.WidthProperty))
' Create a storyboard to contain the animation.
myWidthAnimatedButtonStoryboard = New Storyboard()
myWidthAnimatedButtonStoryboard.Children.Add(myDoubleAnimation)
myStackPanel.Children.Add(myWidthAnimatedButton)
'
' Create and animate the second button.
'
' Create a second button.
myColorAnimatedButton = New Button()
myColorAnimatedButton.Height = 30
myColorAnimatedButton.Width = 200
myColorAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left
myColorAnimatedButton.Content = "Another Button"
' Create a SolidColorBrush to paint the button's background.
Dim myBackgroundBrush As New SolidColorBrush()
myBackgroundBrush.Color = Colors.Blue
' Because a Brush isn't a FrameworkElement, it doesn't
' have a Name property to set. Instead, you just
' register a name for the SolidColorBrush with
' the page where it's used.
Me.RegisterName("myAnimatedBrush", myBackgroundBrush)
' Use the brush to paint the background of the button.
myColorAnimatedButton.Background = myBackgroundBrush
' Create a ColorAnimation to animate the button's background.
Dim myColorAnimation As New ColorAnimation()
myColorAnimation.From = Colors.Red
myColorAnimation.To = Colors.Blue
myColorAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(7000))
' Configure the animation to target the brush's Color property.
Storyboard.SetTargetName(myColorAnimation, "myAnimatedBrush")
Storyboard.SetTargetProperty(myColorAnimation, New PropertyPath(SolidColorBrush.ColorProperty))
' Create a storyboard to contain the animation.
myColorAnimatedButtonStoryboard = New Storyboard()
myColorAnimatedButtonStoryboard.Children.Add(myColorAnimation)
myStackPanel.Children.Add(myColorAnimatedButton)
Me.Content = myStackPanel
End Sub
' Start the animation when the button is clicked.
Private Sub myWidthAnimatedButton_Loaded(ByVal sender as object, ByVal args as RoutedEventArgs) Handles myWidthAnimatedButton.Click
myWidthAnimatedButtonStoryboard.Begin(myWidthAnimatedButton)
End Sub
' Start the animation when the button is clicked.
Private Sub myColorAnimatedButton_Loaded(ByVal sender as object, ByVal args as RoutedEventArgs) Handles myColorAnimatedButton.Click
myColorAnimatedButtonStoryboard.Begin(myColorAnimatedButton)
End Sub
End Class
End Namespace
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
{
// Uses a storyboard to animate the properties
// of two buttons.
public class StoryboardExample : Page
{
public StoryboardExample()
{
// Create a name scope for the page.
NameScope.SetNameScope(this, new NameScope());
this.WindowTitle = "Animate Properties using Storyboards";
StackPanel myStackPanel = new StackPanel();
myStackPanel.MinWidth = 500;
myStackPanel.Margin = new Thickness(30);
myStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "Storyboard Animation Example";
myStackPanel.Children.Add(myTextBlock);
//
// Create and animate the first button.
//
// Create a button.
Button myWidthAnimatedButton = new Button();
myWidthAnimatedButton.Height = 30;
myWidthAnimatedButton.Width = 200;
myWidthAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left;
myWidthAnimatedButton.Content = "A Button";
// Set the Name of the button so that it can be referred
// to in the storyboard that's created later.
// The ID doesn't have to match the variable name;
// it can be any unique identifier.
myWidthAnimatedButton.Name = "myWidthAnimatedButton";
// Register the name with the page to which the button belongs.
this.RegisterName(myWidthAnimatedButton.Name, myWidthAnimatedButton);
// Create a DoubleAnimation to animate the width of the button.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 200;
myDoubleAnimation.To = 300;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
// Configure the animation to target the button's Width property.
Storyboard.SetTargetName(myDoubleAnimation, myWidthAnimatedButton.Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Button.WidthProperty));
// Create a storyboard to contain the animation.
Storyboard myWidthAnimatedButtonStoryboard = new Storyboard();
myWidthAnimatedButtonStoryboard.Children.Add(myDoubleAnimation);
// Animate the button width when it's clicked.
myWidthAnimatedButton.Click += delegate(object sender, RoutedEventArgs args)
{
myWidthAnimatedButtonStoryboard.Begin(myWidthAnimatedButton);
};
myStackPanel.Children.Add(myWidthAnimatedButton);
//
// Create and animate the second button.
//
// Create a second button.
Button myColorAnimatedButton = new Button();
myColorAnimatedButton.Height = 30;
myColorAnimatedButton.Width = 200;
myColorAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left;
myColorAnimatedButton.Content = "Another Button";
// Create a SolidColorBrush to paint the button's background.
SolidColorBrush myBackgroundBrush = new SolidColorBrush();
myBackgroundBrush.Color = Colors.Blue;
// Because a Brush isn't a FrameworkElement, it doesn't
// have a Name property to set. Instead, you just
// register a name for the SolidColorBrush with
// the page where it's used.
this.RegisterName("myAnimatedBrush", myBackgroundBrush);
// Use the brush to paint the background of the button.
myColorAnimatedButton.Background = myBackgroundBrush;
// Create a ColorAnimation to animate the button's background.
ColorAnimation myColorAnimation = new ColorAnimation();
myColorAnimation.From = Colors.Red;
myColorAnimation.To = Colors.Blue;
myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(7000));
// Configure the animation to target the brush's Color property.
Storyboard.SetTargetName(myColorAnimation, "myAnimatedBrush");
Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));
// Create a storyboard to contain the animation.
Storyboard myColorAnimatedButtonStoryboard = new Storyboard();
myColorAnimatedButtonStoryboard.Children.Add(myColorAnimation);
// Animate the button background color when it's clicked.
myColorAnimatedButton.Click += delegate(object sender, RoutedEventArgs args)
{
myColorAnimatedButtonStoryboard.Begin(myColorAnimatedButton);
};
myStackPanel.Children.Add(myColorAnimatedButton);
this.Content = myStackPanel;
}
}
}
전체 샘플을 보려면 속성 애니메이션 샘플을 참조하십시오. 애니메이션 및 Storyboard에 대한 자세한 내용은 애니메이션 개요를 참조하십시오.
코드를 사용하는 경우 속성에 애니메이션 효과를 주기 위해 Storyboard 개체 외에도 다른 방법을 사용할 수 있습니다. 자세한 내용 및 예제를 보려면 방법: Storyboard를 사용하지 않고 속성에 애니메이션 효과 주기 및 방법: AnimationClock을 사용하여 속성에 애니메이션 효과 적용을 참조하십시오.