您好,登錄后才能下訂單哦!
小編給大家分享一下WPF如何實現左右移動動畫效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
本文實例為大家分享了WPF實現左右移動效果展示的具體代碼,供大家參考,具體內容如下
實現控件或布局的左右移動(晃動)主要用到DoubleAnimation以及Storyboard
布局代碼為:
<Canvas> <Grid Width="200" Height="100" Background="MediumAquamarine" Name="GroupboxArea" Canvas.Left="100" Canvas.Top="200"/> <Button Content="Button" Height="25" Width="78" Click="Button_Click"/> </Canvas>
后臺代碼為:
private void Button_Click(object sender, RoutedEventArgs e) { DoubleAnimation DAnimation = new DoubleAnimation(); DAnimation.From = 100;//起點 DAnimation.To = 280;//終點 DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//時間 Storyboard.SetTarget(DAnimation, GroupboxArea); Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty)); Storyboard story = new Storyboard(); story.Completed += new EventHandler(story_Completed);//完成后要做的事 //story.RepeatBehavior = RepeatBehavior.Forever;//無限次循環,需要的自己加上 story.Children.Add(DAnimation); story.Begin(); } void story_Completed(object sender, EventArgs e) { DoubleAnimation DAnimation = new DoubleAnimation(); DAnimation.From = 280;//起點 DAnimation.To = 100;//終點 DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//時間 Storyboard.SetTarget(DAnimation, GroupboxArea); Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty)); Storyboard story = new Storyboard(); story.Completed += new EventHandler(storyCompleted);//完成后要做的事 //story.RepeatBehavior = RepeatBehavior.Forever;//無限次循環,需要的自己加上 story.Children.Add(DAnimation); story.Begin(); } void storyCompleted(object sender, EventArgs e) { DoubleAnimation DAnimation = new DoubleAnimation(); DAnimation.From = 100;//起點 DAnimation.To = 200;//終點 DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//時間 Storyboard.SetTarget(DAnimation, GroupboxArea); Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty)); Storyboard story = new Storyboard(); //story.Completed += new EventHandler(storyCompleted);//完成后要做的事 //story.RepeatBehavior = RepeatBehavior.Forever;//無限次循環,需要的自己加上 story.Children.Add(DAnimation); story.Begin(); }
以上是“WPF如何實現左右移動動畫效果”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。