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