使用StoryBoard執行動畫

流火行者發表於2015-03-21

  在WPF動畫程式設計中,最常用的動畫處理方式是DoubleAnimation動畫,但是隨著你的開發經驗越來越多,你會發現,有時候使用這個動畫類會很麻煩,因為這個動畫是封閉動畫,也就是說在動畫的時間間隔內,你不能對它做對應依賴屬性的賦值操作,這讓你很多時候止步於很好的想法。

  因為我們要求某個時刻動畫能停止,屬效能賦值新的值。所以WPF中提供StoryBoard,這個和IOS中StoryBoard概念有點像,故事版嘛,就是一大堆事情放在同一塊進行統一處理,例如你對某個物件坐了多個屬性的動畫,這些動畫就可以用一個StoryBoard統一管理,極其方便。下面博主寫了一個StoryBoard的動畫,描述一個矩形位置移動,相信在你以後的開發當中會經常用到。

 1  public partial class Window1 : Window
 2     {
 3         public Window1()
 4         {
 5             InitializeComponent();
 6             Loaded += new RoutedEventHandler(Window1_Loaded);
 7         }
 8         Storyboard _sb = new Storyboard();
 9         Rectangle rect1 = new Rectangle();
10 
11         void Window1_Loaded(object sender, RoutedEventArgs e)
12         {
13             DoubleAnimation da = new DoubleAnimation()
14             {
15                 From = 0,
16                 To = 300,
17                 FillBehavior = FillBehavior.Stop,
18                 //AutoReverse = true,
19                 //RepeatBehavior = RepeatBehavior.Forever,
20                 Duration = TimeSpan.FromSeconds(2)
21             };
22             DoubleAnimation da1 = new DoubleAnimation()
23             {
24                 From = 0,
25                 FillBehavior = FillBehavior.Stop,
26                 To = 300,
27                 //RepeatBehavior = RepeatBehavior.Forever,
28                 Duration = TimeSpan.FromSeconds(2)
29             };
30             Rectangle rect = new Rectangle();
31 
32             rect.Width = rect.Height = 100;
33             rect.Fill = Brushes.Blue;
34             _canvas.Children.Add(rect);
35 
36             rect1.Width = rect1.Height = 100;
37             rect1.Fill = Brushes.Red;
38             _canvas.Children.Add(rect1);
39 
40             _sb.Children.Add(da);
41             Storyboard.SetTarget(da, rect);
42             Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.LeftProperty));
43             _sb.Begin();
44         }
45         /// <summary>
46         /// 開始動畫
47         /// </summary>
48         /// <param name="sender"></param>
49         /// <param name="e"></param>
50         private void Button_Click(object sender, RoutedEventArgs e)
51         {
52             bsCopy = _sb.Clone();
53             bsCopy.Children.Clear();
54             foreach (var item in _sb.Children)
55             {
56 
57                 bsCopy.Children.Add(item);
58                 Storyboard.SetTarget(item, rect1);
59                 Storyboard.SetTargetProperty(item, Storyboard.GetTargetProperty(item));
60             }
61             bsCopy.FillBehavior = FillBehavior.Stop;
62             bsCopy.Completed += delegate
63             {
64                 int a = 3;
65                 Canvas.SetLeft(rect1, 100);
66             };
67             bsCopy.Begin();
68         }
69         /// <summary>
70         /// 停止動畫
71         /// </summary>
72         Storyboard bsCopy;
73         private void Button_Click_1(object sender, RoutedEventArgs e)
74         {
75             bsCopy.Stop();
76             Canvas.SetLeft(rect1, 100);
77         }
78     }
View Code

 

相關文章