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实现左右移动(晃动)动画效果到此结束。抉择是一种智慧,更是一种心的写照。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
WPF实现进度条实时更新效果

WPF实现转圈进度条效果

WPF水珠效果按钮组的实现好代码教程

WPF实现画线动画效果

WPF图片按钮的实现方法