本文共 2765 字,大约阅读时间需要 9 分钟。
界面代码(xaml):
后台代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace TransAnimation{ ////// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { private bool _Expand = true; public MainWindow() { InitializeComponent(); } private void Button_Click_1(object sender, RoutedEventArgs e) { Expand = !_Expand; } public bool Expand { get { return _Expand; } set { _Expand = value; Duration duration = new Duration(TimeSpan.FromSeconds(2)); FillBehavior behavior = FillBehavior.HoldEnd; DoubleAnimation translateAnim = new DoubleAnimation(); translateAnim.Duration = duration; translateAnim.FillBehavior = behavior; RectAnimation clipAnim = new RectAnimation(); clipAnim.Duration = duration; clipAnim.FillBehavior = behavior; double delta = 40; //收缩的大小 if (_Expand) // Expand { translateAnim.From = -delta; translateAnim.To = 0; clipAnim.From = new Rect(delta, 0, Thumb1.ActualWidth, Thumb1.ActualHeight); clipAnim.To = new Rect(0, 0, Thumb1.ActualWidth, Thumb1.ActualHeight); } else //Shrink { translateAnim.From = 0; translateAnim.To = -delta; clipAnim.From = new Rect(0, 0, Thumb1.ActualWidth, Thumb1.ActualHeight); clipAnim.To = new Rect(delta, 0, Thumb1.ActualWidth, Thumb1.ActualHeight); } spt1.BeginAnimation(TranslateTransform.XProperty, translateAnim); spc1.BeginAnimation(RectangleGeometry.RectProperty, clipAnim); } } }}
转载地址:http://mgbeo.baihongyu.com/